Net Core 6+ Angular请求带有NULL属性到达
嗨,我有一个具有Angular 12的Net Core 6项目。
我有一个带有模型请求的登录控制器。
当我通过Swagger或Postman执行请求时,它可以正常运行,但是当我从Angular App发送请求时,所有属性所有属性到达无效。
有人知道那会是什么吗?我已经调查了,但没有案件达到我的解决方案。
我的模型:
public class AuthRequest
{
public string Username {get; set;}
public string Password { get; set; }
}
我的控制器:
[AllowAnonymous]
[Route("api/auth")]
public class AuthController : ControllerBase
{
private readonly iAuthService _authService;
public AuthController(iAuthService authService)
{
_authService = loginService;
}
[HttpPost]
public async Task<IActionResult> Auth(AuthRequest request)
{
var response = await _authService.LoginAsync(request);
return Ok(response);
}
}
Postman/Swagger的请求成功:
来自Angular的请求不好:
角服务:
login(payload: AuthModel): Observable<any> {
//payload = { Username: 'someusername', Password: 'somePassword' }
return this.httpClient.post(`${this.baseUrl}/auth`, payload);
}
Hi i've a net core 6 project with angular 12.
i've a login controller with his model request.
When I execute the request through swagger or postman it arrives fine, but when I send the request from my angular app all the properties arrive null.
Anyone know what can it be? I have investigated but no case reaches my solution.
my model:
public class AuthRequest
{
public string Username {get; set;}
public string Password { get; set; }
}
my controller:
[AllowAnonymous]
[Route("api/auth")]
public class AuthController : ControllerBase
{
private readonly iAuthService _authService;
public AuthController(iAuthService authService)
{
_authService = loginService;
}
[HttpPost]
public async Task<IActionResult> Auth(AuthRequest request)
{
var response = await _authService.LoginAsync(request);
return Ok(response);
}
}
request from postman/swagger successful:
request from angular is bad:
Angular service:
login(payload: AuthModel): Observable<any> {
//payload = { Username: 'someusername', Password: 'somePassword' }
return this.httpClient.post(`${this.baseUrl}/auth`, payload);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,当您在没有适当内容类型的情况下发送数据时,就会发生这种情况。
尝试设置HTTP客户端内容类型和Accept标头。
Usually, this happens when you're sending the data without the proper content type.
Try setting the HTTP client content type and the accept headers, like this.