Reputation: 1560
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)
{
}
}
Upvotes: 2
Views: 315
Reputation: 30056
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)
{
}
Upvotes: 1