1

I am working on .net web api in Asp.net Mvc5 project...

By default it returns xml or json...in my project json/xml data returning fine...

I am looking for the setting so that i can get response in JSONP....

Web Api config :

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "SmallBizApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
 config.Formatters.Clear();
        // config.Formatters.Add(new XmlMediaTypeFormatter());
       // config.Formatters.Insert(0, new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));
        config.Formatters.Add(new JsonMediaTypeFormatter());
       // config.Formatters.Add(new XmlMediaTypeFormatter());
    }

I am new to webapi's and looking for a way to do this please help.... Thanks for your time....

1 Answer 1

1

try replacing

config.Formatters.Add(new JsonMediaTypeFormatter());

with

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

i did this on a recent project. and it worked just fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.