Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

c# - HTTP 400: "Input string is not a valid number."

I'm working on an Angular site using an ASP.Net backend to access the database, and the area where I am receiving this error is a custom Async Validator on a Form Control. The purpose of the validator is to check if an input ID exists in a database before allowing the user to submit the form.

The odd thing, though, is that I don't always receive this error when validating input and it works perfectly for the most part. The one case I've found where this occurs is if the string starts in "0" and includes "8" or "9" in any of the following positions, if the string starts with any other number then it does not throw an HTTP 400 error.

In the case of "01823" I get the error "{"":["Input string '01823' is not a valid number. Path '', line 1, position 5."]}" with the message "Http failure response for https://localhost:5001/api/FS/vendor: 400 Bad Request". The position in the error is always the last character in the string

There do exist entries in the database fitting these criteria, the error occurs whether or not the input exists.

Validation Method

// Validate Vendor ID
invalidVendor(): AsyncValidatorFn {
  return (control: AbstractControl): | Observable<{ [key: string]: any } | null> | Promise<{ [key: string]: any } | null> => {
    if (control.value === null || control.value === '') {
      return of(null);
    }
    else {
      return control.valueChanges.pipe(
        debounceTime(500),
        take(1),
        switchMap(_ =>
          this.dbService.checkVendor(control.value).pipe(map(v =>
            v == '' ? { invalidVendor: { value: control.value } } : null)
          )
        )
      );
    }
  };
}

dbService.checkVendor

// Check for valid vendor ID
public checkVendor(vendor: string) {
  var httpOptions = {
    headers: { 'Content-Type': 'application/json' },
    responseType: 'text' as 'json'
  };
  return this.http.post<string>('api/FS/vendor', vendor, httpOptions);
}

FsController.ValidVendor

// POST: api/FS/vendor
[HttpPost("vendor")]
public async Task<ActionResult<string>> ValidVendor([FromBody] string vendor)
{
  var result = await _fsContext.FS_Vendor.Where(x => x.VendorID.Equals(vendor)).FirstOrDefaultAsync();
  return result != null ? result.VendorID : "";
}

UPDATE:

I've fixed the issue by creating an object instead of using a string but I still have no clue why this particular issue happened so I'll keep this open.

question from:https://stackoverflow.com/questions/65830036/http-400-input-string-is-not-a-valid-number

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...