Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net 4.5 ASP.Net web API JSONP support

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.

like image 879
user2526824 Avatar asked Jun 27 '13 06:06

user2526824


People also ask

How do I fix the CORS issue in Web API?

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.

How do I enable cross origin requests in Web API?

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... }

Which of the following protocols does Web API support?

Web APIs are APIs that can be accessed using the HTTP protocol.


1 Answers

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

like image 180
mbudnik Avatar answered Nov 02 '22 02:11

mbudnik