1

Per the WebApiContrib.Formatting.Jsonp GitHub readme, it appears that in the RouteConfig.cs this should be entered:

routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}/{format}",
defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional }
);

I currently don't have a RouteConfig.cs file in my AppStart. I created it using the Web API 2 template and I don't think I changed anything structurally. I do have a WebApiConfig.cs where I have set:

public static void Register (HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();
}

how do I include it such that all routes have the ability to return Jsonp?

Robert
  • 1,745
  • 5
  • 34
  • 61

2 Answers2

1

You could create a custom route attribute which implements IHttpRouteInfoProvider (which Web API route builder looks for when adding routes to route table) and then modify the template that is being generated by appending {format}

Example:

[RoutePrefix("api/values")]
public class ValuesController : ApiController
{
    [CustomRoute(Order = 1)]
    public IEnumerable<string> GetAll()
    {
        return new string[] { "value1", "value2" };
    }

    [CustomRoute("{id}")]
    public string GetSingle(int id)
    {
        return "value";
    }
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public sealed class CustomRouteAttribute : Attribute, IHttpRouteInfoProvider
{
    public CustomRouteAttribute()
    {
        Template = String.Empty;
    }

    public CustomRouteAttribute(string template)
    {
        if (template == null)
        {
            throw new ArgumentNullException("template");
        }

        if (template == string.Empty)
        {
            Template = template + "{format?}";
        }
        else
        {
            Template = template.TrimEnd('/') + "/{format?}";
        }
    }

    public string Name { get; set; }

    public int Order { get; set; }

    public string Template { get; private set; }
}
Kiran
  • 56,921
  • 15
  • 176
  • 161
  • Looking at this now. Could you explain this a bit more? Does the jQuery client do anything other than request jsonp? Not sure where the {format} part comes into play. – Robert Nov 13 '13 at 17:44
  • I don't know how JsonP actually works :-)...probably webapicontrib is the place to find more about this...i just wanted to show how you can enable modifying the template... – Kiran Nov 13 '13 at 17:52
  • I would suggest to open a different question regarding understading jsonp and webapicontrib implementatoin. – Kiran Nov 13 '13 at 19:08
  • I had previously posted about this to the issues section of the Github site for the project (https://github.com/WebApiContrib/WebApiContrib.Formatting.Jsonp/issues/19), was just trying here unless someone else has run into it. – Robert Nov 14 '13 at 14:18
0

I found this comment in a pull request but I don't understand if this is yet implemented into the production package nor if it got pulled at all.

If you are using Attribute Routing, you should add "/{format}" after each route if you plan to use the URI mapping for jsonp, e.g. [Route("api/value/{id:int}/{format?}")]. If you will require the Content-Type header to specify text/javascript, then you can leave your routes alone. (See the sample applications for examples.)

mono68
  • 2,080
  • 19
  • 27