methgaard methgaard - 11 months ago 58
ASP.NET (C#) Question

CORS response to preflight request when endpoint takes argument

i have an Angular2 application and a ASP.NET web api 2 with cors enabled globally:
enter image description here

and a controller with a post, taking a login model (email and password as strings)enter image description here

and the request from client:
enter image description here

If i leave the parameter empty, i have no problem calling it from my frontend, but if i leave it to require the loginmodel, i get the following:
enter image description here
and the request:
enter image description here

Can someone tell how this can be? And not at least how to solve it.

Answer Source

I Cannot explain this, but it worked. I removed:

          var cors = new EnableCorsAttribute("*", "*", "*");

From my WebApiConfig and instead added the following to my web.config:

    <!-- This enables your CORS Requests to be accepted. -->
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Credentials" value="true" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />

The reason why i did not do this from the beginnig was that my Web api 2 did not have the system.webserver, it has an system.web, and i was not aware i could have both. If anyone can bring some sense to this, please do