Does anyone know if returning JSONP
is supported out of the box in .net 4.5 ASP.NET WEB API?
I've found plenty of "roll your own" for earlier versions of MVC
or .net but there doesn't seem to be anything specific to later versions.
I realize this could be because they earlier versions will work with .net 4.5 stack but I'm curious if someone has already been baked in.
You can enable CORS per action, per controller, or globally for all Web API controllers in your application. To enable CORS for a single action, set the [EnableCors] attribute on the action method. The following example enables CORS for the GetItem method only.
To enable cross-origin requests, add the [EnableCors] attribute to your Web API controller or controller method: [EnableCors(origins: "http://example.com", headers: "*", methods: "*")] public class TestController : ApiController { // Controller methods not shown... }
Web APIs are APIs that can be accessed using the HTTP protocol.
As far as I know you must get the jsonp formatter. Here is one of the implementations:
http://nuget.org/packages/WebApi.JsonP
UPDATE
The recommended package is provided now by WebApiContrib team:
https://www.nuget.org/packages/WebApiContrib.Formatting.Jsonp
Add it to Global.asax on application start:
GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));
Example of usage with jquery can be found in here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With