0

I have a WebApi project with controllers that return JSONP. For this I'm using a JsonPMediaTypeFormatter class - I now have to merge these controllers with a 2nd Web Api project which doesn't use custom formatters. How do deliver JSONP for particular routes ie

"api/ProductsController/{action}/{id} - returns JSONP

"api/LookupController/{action}/{id} - no custom formatter

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

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

        config.Formatters.Insert(0, new JsonpMediaTypeFormatter());
     }

1 Answer 1

2

There isn't specifically a per-route way of having formatters, but there is a per-controller configuration which you can use to have specific formatters. For example, in your scenario you could have a common base controller for certain set of controllers and decorate that base controller with the per-controller configuration attribute.

Web API Sample for creating a per-controller configuration: http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/ControllerSpecificConfigSample/ReadMe.txt

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

1 Comment

Link is no longer valid

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.