gpt4 book ai didi

c# - ASP.NET 核心中间件

转载 作者:行者123 更新时间:2023-11-30 17:34:57 26 4
gpt4 key购买 nike

我已经构建了自定义身份中间件。在 Invoke 方法中,我正在检查请求是否有 token 等。检查 token 后,我想将请求进一步传递给 Controller ​​。它为 GET 请求工作 - 它跳转到 Controller 方法。它不适用于 POST 请求。

调用方法

public async Task Invoke(HttpContext context)
{
//checking
await _next(context);
}

其工作 Controller :

[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[AllowAnonymous]
[HttpGet]
public string Get()
{
return "Allow annymous";
}
}

不工作

[Route("api/[controller]")]
public class AccountController : Controller
{
[AllowAnonymous]
[HttpPost]
public void Login()
{
//some logic
HttpContext.Response.WriteAsync("Unauthorized");
}
}

Making POST call Postman returns 404 not found.

最佳答案

相关操作的路径是 /api/Account。确保这是正确的。

如果您想要 /api/Account/Login:

[Route("api/[controller]/[action]")]
public class AccountController : Controller
{
[AllowAnonymous]
[HttpPost]
public void Login()
{
//some logic
HttpContext.Response.WriteAsync("Unauthorized");
}
}

关于c# - ASP.NET 核心中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41766835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com