Skip to main content

How to enable CORS in web API2

In this post I am going to show you a very simple and useful tips that how to enable CORS in web API2.
What is CORS
Cross-origin resource sharing (CORS) is a mechanism that allows many resources (e.g. fonts, JavaScript, etc.) on a web page to be requested from anotherdomain outside the domain from which the resource originated
How to enable in CORS in web API2
In Visual Studio, from the Tools menu, select Library Package Manager, then select Package Manager Console. In the Package Manager Console window, type the following command:
Install-Package Microsoft.AspNet.WebApi.Cors
Open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method.
 public static void Register(HttpConfiguration config)
        {
            // New code
            config.EnableCors();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }

Comments

  1. well this was very nice article and very good information for ASP.NET learners i was really satisfied see this information we also provide ASP.NET Online Training

    ReplyDelete

Post a Comment

Popular posts from this blog