【发布时间】:2019-04-02 08:38:41
【问题描述】:
下面是一个 Asp.net Core WebAPI,当我们说重复用户尝试注册时,它会返回错误的请求以及关于其参数的错误详细信息。
public async Task<IActionResult> Register([FromBody] RegisterModel registerModel)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
//TODO: Use Automapper instead of manual binding
UserName = registerModel.Username,
FirstName = registerModel.FirstName,
LastName = registerModel.LastName,
Email = registerModel.Email
};
var identityResult = await this.userManager.CreateAsync(user, registerModel.Password);
if (identityResult.Succeeded)
{
await signInManager.SignInAsync(user, isPersistent: false);
return Ok(GetToken(user));
}
else
{
Console.WriteLine("Errors are : "+ identityResult.Errors);
return BadRequest(identityResult.Errors);
}
}
return BadRequest(ModelState);
响应在 Angular 端处理如下:
user.service.ts
register(user: User) {
// let headers = new Headers({ 'Content-Type': 'application/json' });
//var reqHeader = new HttpHeaders({ 'Content-Type': 'application/json'});
const reqHeader = new HttpHeaders().set('Content-Type', 'application/json')
.set('Accept', 'application/json');
//return this.http.post(this.rootUrl + '/api/account/register', body,{headers : reqHeader});
return this.http.post(this.apiUrl+ '/api/account/register', user,{headers : reqHeader});
}
上面的方法被调用:
register.component.ts
this.userService.register(this.registerForm.value)
.pipe(map((res: Response) => res.json()))
.subscribe(
data => {
this.alertService.success('Registration successful', true);
this.router.navigate(['/login']);
},
(error:HttpErrorResponse) => {
// let validationErrorDictionary = JSON.parse(error.text());
// for (var fieldName in validationErrorDictionary) {
// if (validationErrorDictionary.hasOwnProperty(fieldName)) {
// this.errors.push(validationErrorDictionary[fieldName]);
// }
// }
// this.alertService.errorMsg(this.errors);
console.log(error.error);
});
当我尝试做邮递员时,我得到了如下完美的结果:
邮递员结果:
但尽管尝试了多个代码 sn-p 仍然没有结果,但结果相同,所有它都会记录“错误结果”作为响应。
角度结果:
虽然响应位于网络选项卡中,但我确实注意到了.. 只是缺少处理它的想法。
错误描述:
这里缺少什么?非常感谢您的回复!
【问题讨论】:
-
你在邮递员结果中得到什么 HTTP 状态码?
-
400 错误结果。
-
我的应用中出现同样的问题,但仍然没有答案...
-
您找到解决方案了吗?
标签: angular asp.net-web-api asp.net-core jwt access-token