7 Feb

Enable Cross-Origin Requests CORS in ASP.NET Web API

For enabling the CORS request in the ASP.NET Web API project we have to download the cors package from the Nuget i.e. Microsoft.AspNet.WebApi.Cors

Open up the Nuget Package Manager console from the Visual Studio Tools Option —> Library Package Manager —> Package Manager Console

nuget package

nuget package

Type following command to install Cors Package

   Install-Package Microsoft.AspNet.WebApi.Cors

The above  CORS package installs, along with any dependencies, into the ASP.NET Web API WebService project. Now open the file App_Start/WebApiConfig.cs and add the following code to the WebApiConfig.Register method.

using System.Web.Http;
namespace WebService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // New code
            config.EnableCors();

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

Next, add the [EnableCors] attribute to the Controller class,  as follows

Enable Cors Per Controller

using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebAPI.Controllers
{
    [EnableCors(origins: "http://www.venkatbaggu.com", headers: "*", methods: "*")]
    public class APIHomeController : ApiController
    {
       Public string Get(int id)
       {
           return "value";
       }
    }
}

If you set [EnableCors] attribute controller class then it will apply action result methods.

For disabling the Cors set the [DisableCors] attribute  for the particular controller or action method.

 

Enable Cors Per Action

using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebAPI.Controllers
{

    public class APIHomeController : ApiController
    {
      [EnableCors]
      Public string Get(int id)
       {
           return "value";
       }
    } 
}

For Enable Cors for the entire ASP.NET Web API project

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute("www.venkatbaggu.com", "*", "*");
        config.EnableCors(cors);
    }
}

4 thoughts on “Enable Cross-Origin Requests CORS in ASP.NET Web API

  1. I have added above those.It is working when it is in localhost, but getting problem after hosting, error is “No ‘Access-Control-Allow-Origin’ header is present on the requested resource”.Please let me know is there any other problem with web.config or iis configure after hosting

    • Did u changed the origins URL?

      [EnableCors(origins: “http://www.example.com”, “*”, “*”)]
      public class APIHomeController : ApiController
      {
      Public string Get(int id)
      {
      return “value”;
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>