.net 4.5 ASP.Net web API JSONP support

2019-04-06 04:23发布

问题:

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.

回答1:

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



回答2:

This was the first post I found and was the most helpful but it still left a a few questions and I found a few problems with some of the answers, while I was able to make it work because of all the answers, I wanted to add what I learned because it seem to be high in the search returns.
I installed https://www.nuget.org/packages/WebApiContrib.Formatting.Jsonp and was able to get it to work after I added

GlobalConfiguration.Configuration.Formatters.Insert(0,  new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));

to my Global.asax on application startas Alex Wheat and mbudnik answered, it caused the CORS to stop working on the other parts of my API that have all ready been implemented.

I now have it all working properly and figured out a few things from the package developer's github.

after installing the nuget package, Add the following to your Global.asax file

GlobalConfiguration.Configuration.AddJsonpFormatter();

then if you call the service using the below Jquery, just replace the url with your url and you can see the results in you console. I also recommend getting Fiddler Web Debugger installed because it will help you troubleshoot.

$.ajax({
    url: 'http://jsfiddle.net/echo/jsonp/',
   type: 'GET',
    contentType: 'text/javascript',
    crossDomain: true,
    success: function(data) {
        console.log(data);
    },
    error: function(jqXHR,status,error) {
    console.log(error);
    }
});

The contentType: 'text/javascript' is important. This is what tells the web api to use the Jsonp Formatter.

Do not include a dataType of 'jsonp' - Example below

     ...type: 'GET',
    dataType: 'jsonp',
    crossDomain: true,... 

this will cause the web api to use the JsonMediaTypeFormatter and you will get the "jQueryrandomfunctionstring was not called parsererror" error. I know this from personal trial and error. Hopefully this helps someone else.



回答3:

I like this WebApiContrib.Formatting.Jsonp NuGet package

Add this formatter into Application_Start:

GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));


回答4:

I also ended up using the WebApiContrib.Formating.Jsonp NuGet Package, but keeping the ability to retun Json without padding if no callback parameter.

So only Jsonp IF the callback parameter i provided.

By adding (from the howTo) to Global.asax.cs - Application_Start()

GlobalConfiguration.Configuration.AddJsonpFormatter();