[英].Net Core 3.1 API Action method with [HTTPPost] returns 405 with incorrect Response Header Allow
I searched around and couldn't find a way to fix my issue.我四处搜索,找不到解决我的问题的方法。 I have actions method below:
我有下面的操作方法:
PostAction method with [Route("api/Deployments")]带有 [Route("api/Deployments")] 的 PostAction 方法
Client Post Request
客户端发布请求
Why is the Response Header Allow does have POST?为什么 Response Header Allow 有 POST?
Thank you so much!非常感谢!
You are running to a CORS issue.您遇到了 CORS 问题。 You need to allow AJAX requests from a different domain.
您需要允许来自不同域的 AJAX 请求。 For example:
例如:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
// ...
}
And then apply this policy to controllers or actions as needed:然后根据需要将此策略应用于控制器或操作:
[EnableCors("MyPolicy")]
See How to enable CORS in ASP.NET Core and https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1 for more details and examples.有关更多详细信息和示例,请参阅如何在 ASP.NET Core和https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1 中启用 CORS 。
Generally, and as a security measure, it is not a good practice to allow requests from all domains (*).通常,作为一种安全措施,允许来自所有域 (*) 的请求不是一个好的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.