Asp.net core 捕获并重新在405 HTTP错误上使用自定义json-ASP.NET核心API

Asp.net core 捕获并重新在405 HTTP错误上使用自定义json-ASP.NET核心API,asp.net-core,asp.net-core-2.0,asp.net-core-webapi,asp.net-core-2.1,Asp.net Core,Asp.net Core 2.0,Asp.net Core Webapi,Asp.net Core 2.1,我试图捕获错误405以启动个性化响应,但我无法做到这一点。当我调用该方法时,我得到一个CORS问题的一般错误 //Startup.cs servicesCollection.AddCors(x => { x.AddPolicy(CORS.AllowPutMethod, policyBuilder => { policyBuilder.WithOrigins("http://localhost:4200")

我试图捕获错误405以启动个性化响应,但我无法做到这一点。当我调用该方法时,我得到一个CORS问题的一般错误

//Startup.cs
servicesCollection.AddCors(x =>
{
    x.AddPolicy(CORS.AllowPutMethod,
        policyBuilder =>
        {
            policyBuilder.WithOrigins("http://localhost:4200")
                .WithMethods(HttpMethods.Put).AllowAnyHeader();
        });
    x.AddPolicy(CORS.AllowPostMethod,
        policyBuilder =>
        {
            policyBuilder.WithOrigins("http://localhost:4200")
                .WithMethods(HttpMethods.Post).AllowAnyHeader();
        });
});

public static class CORS
{
    public const string AllowPutMethod = nameof(AllowPutMethod);

    public const string AllowPostMethod = nameof(AllowPostMethod);
}

[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
    // PUT: api/User/5
    [HttpPut("{id}")]
    [EnableCors(CORS.AllowPostMethod)] <=== ERROR HERE!!!
    public void Put(int id, UserDTO currentUser)
    {
    }

}
//Startup.cs
servicesCollection.AddCors(x=>
{
x、 AddPolicy(CORS.AllowPutMethod,
政策制定者=>
{
policyBuilder.WithOriginates(“http://localhost:4200")
.WithMethods(HttpMethods.Put).AllowAnyHeader();
});
x、 AddPolicy(CORS.AllowPostMethod,
政策制定者=>
{
policyBuilder.WithOriginates(“http://localhost:4200")
.WithMethods(HttpMethods.Post).AllowAnyHeader();
});
});
公共静态类CORS
{
public const string AllowPutMethod=nameof(AllowPutMethod);
public const string AllowPostMethod=nameof(AllowPostMethod);
}
[ApiController]
[路由(“api/[控制器]”)]
公共类UserController:ControllerBase
{
//PUT:api/User/5
[HttpPut(“{id}”)]

[EnableCors(CORS.AllowPostMethod)]您应该使用
CORS.AllowPostMethod
而不是
Put
方法上的
CORS.AllowPostMethod

        [HttpPut("{id}")]
    [EnableCors(CORS.AllowPutMethod)] 
    public void Put(int id, UserDTO currentUser)
    {

    }

您好,这也是有意尝试捕获错误“405 methods not allowed”,但我不能这样做。对于
Method not allowed
error,我认为这是一个CORS错误,我已经在这里提交了[]。