You can use MediaTypeFormatter for OData queries. Just add a new class to your project that inherits MediaTypeFormatter . Then add this to your WebApiConfig file in Register:
config.Formatters.Add(new JSONPFormatter(new QueryStringMapping("$format","jsonp","application/javascript")));
If you then query your object using $format=jsonp , it will return the object as JSONP. You can also request it with a content application/javascript type to get a JSONP return.
Here is a complete example for MediaFormatter to return JSONP. You can easily change it for your needs:
using MyProject.Models; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http.Formatting; using System.Net.Http.Headers; using System.ServiceModel.Syndication; using System.Threading.Tasks; using System.Web; using System.Xml; using Newtonsoft.Json; namespace SMSIdent.Modules.Formatter {
Note. I am using Web API 2. I do not know for sure whether it also works in Web Api 1.
source share