Step 1.
Install cors from NeGet
Step 2.
Enable cors in config
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace WebSample { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.EnableCors(); } } }
Step 3
Add cors attribute to your controller
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.Cors; using WebSample.Models; namespace WebSample.Controllers.Api { public class JobController : ApiController { [EnableCors(origins: "*", headers: "*", methods: "GET")] public Message Get() { return new Message { ID = 2, Content = "Job" }; } } }
Step 4
Test x-domain request