Search code examples
c#asp.net-web-apiasp.net-web-api-routing

Web API routing actions


The goal is to allow these four endpoints:

POST    v1/invoices<br/>
POST    v1/invoices/12345<br/>
POST    v1/invoices/12345/attachment<br/>
POST    v1/invoices/12345/image

Routing entries:

routes.MapHttpRoute(
        name: "InvoiceAttachments",
        routeTemplate: "v1/invoices/{id}/attachments",
        defaults: new { controller = "invoices", action = "PostAttachment" }
    );

routes.MapHttpRoute(
        name: "InvoiceImages",
        routeTemplate: "v1/invoices/{id}/images",
        defaults: new { controller = "invoices", action = "PostImage" }
    );

These are my four function definitions in the controller:

[HttpPost]
[ActionName("PostAttachment")]
public HttpResponseMessage PostAttachment(int id)

[HttpPost]
[ActionName("PostImage")]
public HttpResponseMessage PostImage(int id)

[HttpPost]
public HttpResponseMessage Post(int id)    

[HttpPost]
public HttpResponseMessage Post()

Yet when I post an invoice using the first URI, the route that gets recognized is the attachments route. How do i have endpoints with different sections after the ID variable?


Solution

  • Got it!

    Route:

    routes.MapHttpRoute(
            name: "InvoiceStuff",
            routeTemplate: "v1/invoices/{id}/{*action}",
            defaults: new { controller = "invoices", action = "" }
        );
    

    Function definitions:

    [HttpPost]
    [ActionName("Attachments")]
    public HttpResponseMessage Attachments([FromUri]int id)
    
    [HttpPost]
    [ActionName("Images")]
    public HttpResponseMessage Images([FromUri]int id)
    
    [HttpPost]
    public HttpResponseMessage Post()
    
    [HttpPost]
    [ActionName("")]
    public HttpResponseMessage Post([FromUri]int id)
    

    Works perfectly. Note that in the route I specified a default action of "" because of a defect in webAPI routing with wildcards. Had i not done the default, a null reference exception would have been thrown. More information on the defect can be found here: http://aspnetwebstack.codeplex.com/workitem/718 - slated to be fixed in ASP.NET v5.

    Thanks for all your help!