Net Core 6+ Angular请求带有NULL属性到达

发布于 2025-02-13 07:57:09 字数 1412 浏览 1 评论 0原文

嗨,我有一个具有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:

enter image description here

request from angular is bad:

enter image description here

Angular service:

login(payload: AuthModel): Observable<any> {
    //payload = { Username: 'someusername', Password: 'somePassword' }
    return this.httpClient.post(`${this.baseUrl}/auth`, payload);
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2025-02-20 07:57:09

通常,当您在没有适当内容类型的情况下发送数据时,就会发生这种情况。

尝试设置HTTP客户端内容类型和Accept标头。

var headers = new Headers({
    "Content-Type": "application/json",
    "Accept": "application/json"
});

this.http.post(this.oauthUrl, JSON.stringify(postData), {
    headers: headers
})

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.

var headers = new Headers({
    "Content-Type": "application/json",
    "Accept": "application/json"
});

this.http.post(this.oauthUrl, JSON.stringify(postData), {
    headers: headers
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文