Feel i've spent quite a lot of time trying to get this to work. There's a bunch of helpful SO answers on the topic:
- How to support HTTP OPTIONS verb in ASP.NET MVC/WebAPI application
- Enabling CORS with WebAPI PUT / POST requests?
- CORS support for PUT and DELETE with ASP.NET Web API
But... none actually solved mine. I feel it's time to phrase my own question, explain what i've done and what I experience is the issue I have.
- Installed
Microsoft.AspNet.WebApi.Cors
via NuGet - Added attribute
[EnableCors("http://localhost:PORT", "*", "GET,POST,DELETE,OPTIONS")]
on my WebAPI controller.
GET requests works at this point, like a charm.
When I do PUT or DELETE, the pre-flight request fails. I get an internal server error reponse in the client browser. Turns out that the OPTIONS request fails because I have no options method in my web api controller.
By adding the options method as follows:
public HttpResponseMessage Options()
{
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK
};
return response;
}
PUT and DELETE requests does work, hurray!
I'm not sure where to draw the line of the responsibility of the developer vs the framework, but it feels like I should not have to care much about the OPTIONS method. Must I really explicitly specify this method in order for PUT, DELETE, OPTIONS to work?
Thanks for all help!