C # Web Api with CORS enabled and the dangerous header No 'Access-Control-Allow-Origin' is present on the requested resource

I have a C # web API project. I created a controller using the Web API 2 standard. I try to include CORS in only one action, this is what I have:

namespace MyProject.Controllers
{
    public class MyControllerController : ApiController
    {
        // POST api/mycontroller
        [EnableCors("http://link.myurl.com", "*", "*")]
        public bool Post(MyCustomObject customObject)
        {
            // Function that returns Bool
            return myFunction(customObject);
        }
    }
}

My WebApiConfig.cs looks like this:

// Web API configuration and services
config.EnableCors(); 

// Web API routes
config.MapHttpAttributeRoutes();

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

// Ignore Null values on JSON Returns
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore };

Here is an AJAX cross-domain call:

$.ajax({
    type: "POST",
    url: 'http://myurl.com/api/mycontroller',
    data: JSON.stringify({ Type: 'TextHere', OtherInfo: 'TextHere' }),
    contentType: "application/json",
    success: function (data) {
    }
});

Based on tutorials like this one: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api#how-it-works , I have to get everything right tune.

I keep getting the following error in Chrome:

XMLHttpRequest http://myurl.com/api/mycontroller. Access-Control-Allow-Origin . Origin 'http://link.myurl.com' .

Fiddler, , Origin 200, , 500.

, , . , Origin , Allow Origin.

web.config:

<system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="http://link.myurl.com" />
     </customHeaders>
   </httpProtocol>
 <system.webServer>

[EnableCors], , (http://link.myurl.com), . [EnabledCors], .

, . , ( ), JSON.

HELP?

+4
2

, , . , NuGet:

Microsoft ASP.NET Web API 2.2 Cross-Origin

WebApiConfig.cs, , EnableCors , .

web.config.

, - NuGet.

+1

. EnableCORS , .

// Web API routes
config.MapHttpAttributeRoutes();

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

// Web API configuration and services
config.EnableCors(); 
+1

All Articles