2

I follow the step below to solve CORS issue.

1.Install Microsoft.AspNet.WebApi.Cors with nuget.

2.Add code below in WebApiConfig.cs

config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

Most of API will work. Only oData API will still have CORS issue.

How to resolve CORS problem in oData API?

edit:

After input the code below in Global.asax and it works.

        protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Context.Request.Path.Contains("odata/") && Context.Request.HttpMethod == "OPTIONS")
        {
            Context.Response.AddHeader("Access-Control-Allow-Origin", Context.Request.Headers["Origin"]);
            Context.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
            Context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST PUT, DELETE, OPTIONS");
            Context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
            Context.Response.End();
        }
    }
EvaHHHH
  • 293
  • 3
  • 16

1 Answers1

0

Check out this answer where it says that you can try to add:

var cors = new EnableCorsAttribute(
    "http://localhost:7122/",
    "*",
    "*",
    "DataServiceVersion, MaxDataServiceVersion"
);
config.EnableCors(cors);
Tore Nestenius
  • 16,431
  • 5
  • 30
  • 40
  • USe Fiddler and post a trace of the HTTP CORS traffic and also perhaps post the error in the browser console. Also post your complete WebApiConfig method where you configure the various bits and pieces. – Tore Nestenius Aug 05 '20 at 08:57