A JSONP Formatter for ASP.NET Web API
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.nuget Migrate projects from nuget to paket Mar 13, 2015
.paket Migrate projects from nuget to paket Mar 13, 2015
samples Merge branch 'paket_fake_integration' of https://github.com/vba/WebAp… Jun 26, 2015
src/WebApiContrib.Formatting.Jsonp Bump version to 3.0.2 Oct 13, 2015
test/WebApiContrib.Formatting.Jsonp.Tests correct project references Aug 12, 2015
tools Fix Verbose warning. Jan 9, 2014
.gitattributes
.gitignore
LICENSE.txt Update LICENSE.txt Dec 6, 2012
README.md
RELEASE_NOTES.md
WebApiContrib.Formatting.Jsonp.sln Add an ability to push and package nuget packages Mar 16, 2015
build.cmd Add fake build script and fill it with trivial tasks such as build / … Mar 13, 2015
build.fsx
build.sh Add an ability to push and package nuget packages Mar 16, 2015
paket.dependencies
paket.lock Add fake build script and fill it with trivial tasks such as build / … Mar 13, 2015

README.md

WebApiContrib.Formatting.Jsonp

WebApiContrib.Formatting.Jsonp provides a JSONP MediaTypeFormatter implementation for ASP.NET Web API.

In order to add it to your Web API solution, run Install-Package WebApiContrib.Formatting.Jsonp from your NuGet Package Manager console in Visual Studio.

To use the JsonpMediaTypeFormatter, add the following code to your configuration in Global.asax.cs:

GlobalConfiguration.Configuration.AddJsonpFormatter();

You can specify a MediaTypeFormatter and callback parameter name as optional parameters. By default, the JsonpMediaTypeFormatter will use the config.Formatters.JsonFormatter and callback as default values.

You should specify text/javascript the media type to accept in the request's Accept header. If you leave this out, the client may receive application/json as the default JsonMediaTypeFormatter will likely handle the request. In other words, the Accept header is used as the means to signal Web API to use the JsonpMediaTypeFormatter. For example, in JQuery, you would write:

$.ajax({
    type: "GET",
    url: "/jsonp-endpoint?callback=callme",
    contentType: "text/javascript",
    // ...
});

The JsonpMediaTypeFormatter will then match the specified callback parameter name from the request URI, e.g. ?callback=?.

If you are using traditional routing, update your Default ASP.NET Web API route in /App_Start/WebApiConfig.cs:

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

To see the JsonpMediaTypeFormatter in action, just clone this project, run the WebContribContrib.Formatting.Jsonp.SampleWebHost project web application, and then start the WebApiContrib.Formatting.Jsonp.SampleJQueryClient web application and hit the "Get JSONP" button.