AngularJS + ASP.NET Web API Cross-Domain Issue

前端 未结 4 1853
我在风中等你
我在风中等你 2020-12-13 21:49

Moving forward with my mobile app development learning process, I\'ve found a new obstacle: Cross-origin Request Sharing or CORS.

I am using a combination of Angular

4条回答
  •  萌比男神i
    2020-12-13 22:29

    While stumbling onto this issue with AngularJS 1.3 with Microsoft Web API 2 I found a simple solution to the CORS configuration issue.

    • First from Nuget intall - Microsoft WebApi Cors

      Install-Package Microsoft.AspNet.WebApi.Cors
      
    • Then in your WebApiConfig.cs file:

      var cors = new System.Web.Http.Cors.EnableCorsAttribute("www.my-angular-web-site.com", "*", "*");
      
      config.EnableCors(cors);
      

    You can also enable CORS everywhere with a * instead of your web site but that defeats the purpose of CORS and opens up security holes - but you can do it for testing.

    The WebApi.Cors assembly also lets you enable cors controller by controller or with other more granular details. Other details can be found here on the Web API site.

提交回复
热议问题