首页 > 解决方案 > .NET core OPTIONS 请求返回 405 错误

问题描述

我需要将 CORS 支持添加到 .NET core 3.1 API(是 2.2,但我决定同时更新)。我以为这很容易。这里的文档:https ://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1看起来很简单。我想在所有端点上支持相同的 CORS 策略。所以,我想我可以不理会我的控制器,只需对 startup.cs 进行更改。我错过了什么?

我有一个 POST 端点,但 OPTIONS 请求返回 405 错误。我认为 CORS 中间件应该处理请求,这样我就不必在控制器内部考虑 CORS。

启动.cs

  namespace MyAPI
  {
    public class Startup
    {

        readonly String ALLOW_ALL = "AllowAll";
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // services.Configure<IISOptions>(options =>
            // {
            //     options.AutomaticAuthentication = true;
            // });
            services.AddLogging();
            services.AddCors(options =>
            {
                options.AddDefaultPolicy(builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader();
                    // .AllowCredentials();
                });
            });
            services.AddMvc(MvcOptions => MvcOptions.EnableEndpointRouting = false);

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseRouting();

            // if (env.IsDevelopment())
            // {
            // app.UseDeveloperExceptionPage();
            app.UseCors();
            // }

            app.UseEndpoints(endpoints => endpoints.MapControllers());

            app.UseMvc();
        }
    }
}

我的控制器.cs

namespace MyProj.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FacilitiesController : ControllerBase
    {

        public FacilitiesController()
        {
        }


        [HttpPost]
        public JsonResult FloorPlanURL([FromBody] UniqueFloor theFloor)
        {
          <stuff happens>
          return new JsonResult(new {success = true, url = out});
        }

我已经经历了几次迭代,也在这里查看:https ://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1#enable-cors-with-endpoint-routing . 我可能错过了一些小而简单的东西。我可以在所有控制器中添加手动 OPTIONS 处理程序,但这似乎是一个非常糟糕的解决方案。

标签: c#.net-corecorshttp-status-code-405

解决方案


你需要修改你的类 StartUp.cs,试着把代码像这样......

public void ConfigureServices (IServiceCollection services) {
    services.AddLogging ();
    services.AddCors (); // just adding the cors to the services
    services.AddMvc ().SetCompatibilityVersion (CompatibilityVersion.Version_2_2);
}
public void Configure (IApplicationBuilder app, IHostingEnvironment env) {
    // global cors policy
    app.UseCors (x => x
        .AllowAnyOrigin ()
        .AllowAnyMethod ()
        .AllowAnyHeader ()); // configure CORS

    app.UseHttpsRedirection ();
    app.UseMvc ();
}

并且 CORS 必须对您的所有 Apicontroller 正常工作。

如果错误代码 405 继续出现:表示您没有正确调用端点。

请验证并给我有关结果的反馈。


推荐阅读