2

I am trying to capture error 405 to launch a personalized response, but I can not do it. When I make a call to the method, I get a generic error of CORS problem

//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)
    {
    }

}

HTTP ERROR (Angular 6)

Response

1 Answer 1

1

You shoul use CORS.AllowPutMethod instead of CORS.AllowPostMethod on the Put method.

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

    }
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, that is on purpose as well to try to capture the error "405 Methos not allowed", but I can not do it.
For Method not allowed error, I think it is a CORS bug, and I have submitted it here [github.com/aspnet/Home/issues/3327 ].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.