Creating a JSONP Formatter for ASP.NET Web API
Out of the box ASP.NET WebAPI does not include a JSONP formatter, but it's actually very easy to create a custom formatter that implements this functionality.
Why do we need JSONP?
JSONP is one way to allow browser based JavaScript client applications to bypass cross-site scripting limitations and serve data from the non-current Web server. AJAX in Web Applications uses the XmlHttp object which by default doesn't allow access to remote domains. There are number of ways around this limitation <script> tag loading and JSONP is one of the easiest and semi-official ways that you can do this.
JSONP works by combining JSON data and wrapping it into a function call that is executed when the JSONP data is returned. If you use a tool like jQUery it's extremely easy to access JSONP content.
Imagine that you have a URL like this:
http://RemoteDomain/aspnetWebApi/albums
which on an HTTP GET serves some data - in this case an array of record albums. This URL is always directly accessible from an AJAX request if the URL is on the same domain as the parent request. However, if that URL lives on a separate server it won't be easily accessible to an AJAX request.
Now, if the server can serve up JSONP this data can be accessed cross domain from a browser client. Using jQuery it's really easy to retrieve the same data with JSONP:
function getAlbums() { $.getJSON("http://remotedomain/aspnetWebApi/albums?callback=?",null, function (albums) { alert(albums.length); }); }
The resulting callback the same as if the call was to a local server when the data is returned. jQuery deserializes the data and feeds it into the method. Here the array is received and I simply echo back the number of items returned. From here your app is ready to use the data as needed.
What does JSONP look like?
JSONP is a pretty simple 'protocol'. All it does is wrap a JSON response with a JavaScript function call. The above result from the JSONP call looks like this:
Query17103401925975181569_1333408916499( [{"Id":"34043957","AlbumName":"Dirty Deeds Done Dirt Cheap",…},{…}] )
The way JSONP works is that the client (jQuery in this case) sends of the request, receives the response and evals it. The eval basically executes the function and deserializes the JSON inside of the function passing the resulting object as a parameter.
How does JSONP work?
To understand how JSONP works, here's some plain JavaScript code that demonstrates the semantics:
function jsonp(url, callback) { // create a unique id var id = "_" + (new Date()).getTime(); // create a global callback handler window[id] = function (result) { // forward the call to specified handler if (callback) callback(result); // clean up: remove script and id var sc = document.getElementById(id); sc.parentNode.removeChild(sc); window[id] = null; } url = url.replace("callback=?", "callback=" + id); // create script tag that loads the 'JSONP script' // and executes it calling window[id] function var script = document.createElement("script"); script.setAttribute("id", id); script.setAttribute("src", url); script.setAttribute("type", "text/javascript"); document.body.appendChild(script); }
The code creates a script tag that basically loads the JSONP snippet and executed it executes it. The 'code' in this case is a function call, which here is a unique name of the function windows[id] I assigned to handle the callback. This method is fired and the JSON payload is converted to a JavaScript instance when it runs. This generic function then routes final result to the function that was passed in as a parameter which allows you just specify an anonymous function or a function delegate.
To call this from any JavaScript code use the following code:
function getAlbumsManual() { jsonp("http://rasXps/aspnetWebApi/albums?callback=?", function (albums) { alert(albums.length); }); }
This all works fine using either jQuery or this simple JSONP implementation - as long as the server can serve the data with JSONP.
JSONP and ASP.NET Web API
As mentioned previously, JSONP support is not natively in the box with ASP.NET Web API. But it's pretty easy to create and plug-in a custom formatter that provides this functionality.
The following code is based on Christian Weyer's example but has been updated to work with the latest Web API RTM bits, which changes the implementation a bit due to the way dependent objects are exposed differently in the latest builds.
Here's the code:
(updated 8/17/2012 for RTM)
(updated 8/23/2012 per Brad's and BK's comments)
using System; using System.IO; using System.Net; using System.Net.Http.Formatting; using System.Net.Http.Headers; using System.Threading.Tasks; using System.Web; using System.Net.Http; using Newtonsoft.Json.Converters; using System.Web.Http; namespace Westwind.Web.WebApi { /// <summary> /// Handles JsonP requests when requests are fired with text/javascript /// </summary> public class JsonpFormatter : JsonMediaTypeFormatter { public JsonpFormatter() { SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/javascript")); JsonpParameterName = "callback"; } /// <summary> /// Name of the query string parameter to look for /// the jsonp function name /// </summary> public string JsonpParameterName {get; set; } /// <summary> /// Captured name of the Jsonp function that the JSON call /// is wrapped in. Set in GetPerRequestFormatter Instance /// </summary> private string JsonpCallbackFunction; public override bool CanWriteType(Type type) { return true; } /// <summary> /// Override this method to capture the Request object /// </summary> /// <param name="type"></param> /// <param name="request"></param> /// <param name="mediaType"></param> /// <returns></returns> public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, System.Net.Http.HttpRequestMessage request, MediaTypeHeaderValue mediaType) { var formatter = new JsonpFormatter() { JsonpCallbackFunction = GetJsonCallbackFunction(request) }; // this doesn't work unfortunately //formatter.SerializerSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings; // You have to reapply any JSON.NET default serializer Customizations here formatter.SerializerSettings.Converters.Add(new StringEnumConverter()); formatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; return formatter; } public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContent content, TransportContext transportContext) { if (string.IsNullOrEmpty(JsonpCallbackFunction)) return base.WriteToStreamAsync(type, value, stream, content, transportContext); StreamWriter writer = null; // write the pre-amble try { writer = new StreamWriter(stream); writer.Write(JsonpCallbackFunction + "("); writer.Flush(); } catch (Exception ex) { try { if (writer != null) writer.Dispose(); } catch { } var tcs = new TaskCompletionSource<object>(); tcs.SetException(ex); return tcs.Task; } return base.WriteToStreamAsync(type, value, stream, content, transportContext) .ContinueWith( innerTask => { if (innerTask.Status == TaskStatus.RanToCompletion) { writer.Write(")"); writer.Flush(); } },TaskContinuationOptions.ExecuteSynchronously) .ContinueWith( innerTask => { writer.Dispose(); return innerTask; },TaskContinuationOptions.ExecuteSynchronously) .Unwrap(); } /// <summary> /// Retrieves the Jsonp Callback function /// from the query string /// </summary> /// <returns></returns> private string GetJsonCallbackFunction(HttpRequestMessage request) { if (request.Method != HttpMethod.Get) return null; var query = HttpUtility.ParseQueryString(request.RequestUri.Query); var queryVal = query[this.JsonpParameterName]; if (string.IsNullOrEmpty(queryVal)) return null; return queryVal; } } }
Note again that this code will not work with the Beta bits of Web API - it works only with RTM bits.
This code is a bit different from Christians original code as the API has changed. The biggest change is that the Read/Write functions no longer receive a global context object that gives access to the Request and Response objects as the older bits did.
Instead you now have to override the GetPerRequestFormatterInstance() method, which receives the Request as a parameter. You can capture the Request there, or use the request to pick up the values you need and store them on the formatter. Note that I also have to create a new instance of the formatter since I'm storing request specific state on the instance (information whether the callback= querystring is present) so I return a new instance of this formatter.
Other than that the code should be straight forward: The code basically writes out the function pre- and post-amble and the defers to the base stream to retrieve the JSON to wrap the function call into. The code uses the Async APIs to write this data out (this will take some getting used to seeing all over the place for me).
Note that when you enable this formatter it effectively replaces the stock JSON formatter (ie. JSON.NET) because it handles the same media types for writing. This code still uses the same JSON.NET formatter, but it won't be initialized since effectively we are creating a new instance for each JSON and JSONP write request.
This means if you plan to configure the JsonFormatter for *all* JSON configuration you have to do it in this formatter's GetPerRequestFormatterInstance() override.
Hooking up the JsonpFormatter
Once you've created a formatter, it has to be added to the request processing sequence by adding it to the formatter collection. Web API is configured via the static GlobalConfiguration object.
protected void Application_Start(object sender, EventArgs e) {// Verb Routing RouteTable.Routes.MapHttpRoute( name: "AlbumsVerbs", routeTemplate: "albums/{title}", defaults: new { title = RouteParameter.Optional, controller = "AlbumApi" } );GlobalConfiguration .Configuration .Formatters .Insert(0, new Westwind.Web.WebApi.JsonpFormatter()); }
That's all it takes.
Note that I added the formatter at the top of the list of formatters, rather than adding it to the end which is required. The JSONP formatter needs to fire before any other JSON formatter since it relies on the JSON formatter to encode the actual JSON data. If you reverse the order the JSONP output never shows up. So, in general when adding new formatters also try to be aware of the order of the formatters as they are added.
Resources
Other Posts you might also like
The Voices of Reason
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
Will happily contribute once everything syncs up closer to RTM. Already gave permission to allow use when they're ready for it.
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
I attended your jQuery sessions at DevConnections a few years back and as always you present solutions succinctly, thanks for this article. I have a question/criticism: Why isn't this included behavior/functionality included out of the box? According to the ASP .Net website:
"ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices."
However, mobile devices could not consume the service in the case that you're using JSON which is probably what you should be doing for mobile devices (for various reasons). Was this something that Microsoft just completely didn't take into consideration or do you know if navtive JSONP support will come in a future version?
Thanks,
Giancarlo
# re: Creating a JSONP Formatter for ASP.NET Web API
However, it is nice that it's so easy to create this type of functionality with very little effort and plug it in yourself.
# re: Creating a JSONP Formatter for ASP.NET Web API
See also:
- http://msdn.microsoft.com/en-US/library/system.servicemodel.description.webserviceendpoint.crossdomainscriptaccessenabled.aspx
- http://msdn.microsoft.com/en-us/library/ee834511.aspx
- http://www.gutgames.com/post/Enabling-JSONP-in-WCF.aspx
# re: Creating a JSONP Formatter for ASP.NET Web API
http://www.codeproject.com/Articles/417629/Support-for-JSONP-in-WCF-REST-services
# re: Creating a JSONP Formatter for ASP.NET Web API
Error 1 'Westwind.Web.WebApi.JsonpFormatter.WriteToStreamAsync(System.Type, object, System.IO.Stream, System.Net.Http.Headers.HttpContentHeaders, System.Net.TransportContext)': no suitable method found to override C:\Projects-VS2012\TestFullWebsiteStructure\TestWebAPIWebSite\App_Code\JsonpFormatter.cs 85 30 TestWebAPIWebSite
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
https://gist.github.com/3411200
# re: Creating a JSONP Formatter for ASP.NET Web API
After adding the JsonpFormatter to my pipeline, the Json.NET Enum to string Converter has stopped working.
I have this line to add the StringEnumConverter:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());
But with the JsonpFormatter in the beggining of the pipeline, somehow the enum to string conversion has stopped working.
Any ideas?
# re: Creating a JSONP Formatter for ASP.NET Web API
JsonpFormatter inherits from the JSON.NET formatter so you can apply the same properties when the formatter is created. This involves overriding GetPerRequestFormatterInstance():
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, System.Net.Http.HttpRequestMessage request, MediaTypeHeaderValue mediaType) { var formatter = new JsonpFormatter() { JsonpCallbackFunction = GetJsonCallbackFunction(request) }; formatter.SerializerSettings.Converters.Add(new StringEnumConverter()); return formatter; }
And this works. After looking at this I was hoping you could just do:
formatter.SerializerSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
but that seems to have no effect as the added converters are not showing up for some reason (Converters.Count = 0). I'll have to look at this closer another time, but for now it looks like you have to configure the formatter directly with the desired settings in this method.
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
Have you tried using the formatter with a self hosted console application?
I've tried your formatter in a regular MVC 4 project, and it worked immediately. However, I would like to use it in a self hosted console application, and I've had a lot of trouble getting it to work.
I've registered the formatter, and verified that it is added:
var config = new HttpSelfHostConfiguration(serviceUrl); config.Formatters.Insert(0, new JsonpMediaTypeFormatter());
I have verified that it is being called when I make a request with the following code:
$("#TestButton").click(function () { $.ajax({ url: 'http://localhost:8082/Api/Test', type: 'GET', dataType: 'jsonp', success: function(data) { alert(data.TestProperty); } }); });
I've checked in Fiddler, and the response I get is:
HTTP/1.1 504 Fiddler - Receive Failure Content-Type: text/html; charset=UTF-8 Connection: close Timestamp: 09:30:51.813 [Fiddler] ReadResponse() failed: The server did not return a response for this request.
I'd be really grateful if anybody can shed some light on what is going on!
Thanks,
Francis
# re: Creating a JSONP Formatter for ASP.NET Web API
Francis
# re: Creating a JSONP Formatter for ASP.NET Web API
Thanks,
Manuel
# re: Creating a JSONP Formatter for ASP.NET Web API
I send following json request still it doesn't work and also I added JSON Formatter!!
function PostData() {
jQuery.support.cors = true;
var model = {
UserName: "mahdi",
Password: "123"
};
$.ajax({
url: 'http://localhost:6236/api/values/post',
type: 'POST',
data: JSON.stringify(model),
contentType: "application/json;charset=utf-8",
success: function (data) {
alert("success");
$('#divResult').append(data + "(" + data.status + ")");
},
error: function (data) {
alert(data.status + " error");
}
});
}
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
url: '/api/values/post' instead of url: 'http://localhost:6236/api/values/post',
then it works great
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
my problem is that it doesn't work with Chrome and Firefox !!?
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
I am trying to add the JsonpFormatter classs to my code but I am having and error related to override Task WriteToStreamAsync(..) the errro is "C# Not suitable member to override"
Also from this comments(@mahdi - JSONP only works with GET operations - not any other HTTP Verb like POST.) Do you mean with this that the cross domain request suing other http verbs will not work with JSONP?
How can I send information from my other application to my Web API using client side
Thank you
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
Cross domain HTTP from the browser is simply something that you shouldn't plan on using as a design choice. Apps will typically access local server resources, and if they need resources from remote domains, use server side HTTP calls to aggregate the data.
Desktop apps or mobile apps can easily access cross-domain content so this issue is limited to browsers - but APIs are meant for any type of HTTP client.
In the end this is a browser limitation not a limitation of the server software that hosts an API.
# re: Creating a JSONP Formatter for ASP.NET Web API
The WebAPI will be accessed by both the mobile clients that do not use a browser, as well as a browser-based web site that uses javascript controls to read/write to webapi. The issue is with the web site.
To explain the issue in a little more detail:
I am writing the backend using WebAPI on my server, and the web developer is writing the web site using KendoUI JavaScript code - a grid control that reads and writes to my WebAPI from her machine. The machines are on different domains, so a SOP error results. What is the recommended way to get around this?
I am considering using CORS as a workaround, but IE 8 and 9 have limited support for CORS. I found this blog post that discusses how to allow IE 8 and 9 to use CORS:
http://georgedurzi.com/implementing-cross-browser-cors-support-for-asp-net-web-api/
I have yet to figure out how to make the KendoUI grid use the JavaScript transport function code referenced in the post.
As an alternative, I guess we can bind the grid to write data to some local data store that resides on the machine/server which serves the page, and then have the local server pass off that written data to the WebAPI server server-side using HttpClient or something like that.. that just seems the wrong way to do it and an extra step, instead of having the javascript grid post the data straight to WebAPI.
What are your thoughts on doing the server-side HTTP call this way, or the above blog post?
Thank you
# re: Creating a JSONP Formatter for ASP.NET Web API
# re: Creating a JSONP Formatter for ASP.NET Web API
I know its an old thread and thanks for this solution. Just to note the the Dispose() of the StreamWriter() closes the underlying Stream. This can cause problem with other filters where a MemoryStream is passed to WriteToStreamAsync.
.Net 4.5 now has a leaveOpen bool in one of the StreamWriter constructors.
I have removed the Dispose() from the code with no memory or resource issues.
# re: Creating a JSONP Formatter for ASP.NET Web API
[1] http://github.com/WebApiContrib/WebAPIContrib