简体   繁体   English

带有 [HTTPPost] 的 .Net Core 3.1 API Action 方法返回 405 并带有错误的响应头允许

[英].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 方法带有 [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 Corehttps://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.

相关问题 HTTP 错误 405 | [HttpPost] ASP.NET MVC 核心 3.1 - HTTP ERROR 405 | [HttpPost] ASP.NET MVC Core 3.1 .NET Core 3.1 API 方法中的 GZip 响应 - GZip response in API method with .NET Core 3.1 选定的复选框未显示在 asp.net core mvc 3.1 的 HttpPost 操作方法中 - Selected Checkboxes are not showing up in the HttpPost action method in asp.net core mvc 3.1 HTTPPost .net 核心的操作方法覆盖 - Action method overrides for HTTPPost .net core 在 .net 核心 3.1 web Z8A5DA52ED1264417D35AZE 应用程序中将 model 发送到 HttpPost 方法时出现问题 - Problem sending model to HttpPost method in a .net core 3.1 web api application ASP.NET Core 3.1 Web API post 方法导致 405“Method Not Allowed”错误 - ASP.NET Core 3.1 Web API post method causing 405 "Method Not Allowed" error .net core 3.1 mvc razorpage 编辑方法 httppost 不起作用? - .net core 3.1 mvc razorpage edit method httppost not working? Asp.Net Core 3.1 405 方法不允许 - Asp.Net Core 3.1 405 Method Not Allowed .Net Core API 返回 405:Method not allowed 具有 Authorize 属性时 - .Net Core API returns 405:Method not allowed when having Authorize attribute Httppost 和 httpput 被 .net 核心 3.1 中的 CORS 阻止 - Httppost and httpput blocked by CORS in .net core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM