1
votes

I have one api Controller with two different routes:

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

        config.Routes.MapHttpRoute(
            name: "downloadApi",
            routeTemplate: "api/{controller}/{download}/{id}"
            );

I want to have these actions with these routes:

1 - For downloading a file: public GetFile()

GET /api/v1/documents/download/id 

2 - For getting file info: public GetFileInfo()

GET /api/v1/documents/id

So in my controller I want to have these actions:

public class DocumentsController : apiController
{
    // triggers with GET /api/v1/documents/download/id 
    public HttpResponseMessage GetFile(){}

    // triggers with GET /api/v1/documents/id
    public HttpResponseMessage GetFileInfo(){}
 }

How can I do this?

Thanks

1
I believe you'd have to do this via method attributes that define the route for each method.Ben Black

1 Answers

2
votes

Here's an example to explain my comment. Rather than defining a route in your WebApiConfig.cs that applies to some routes, but not others (which I don't think you can do, but I've never tried so maybe you can), use the Route method attribute to define the route used at the method level.

public class DocumentsController : apiController
{
    // triggers with GET /api/v1/documents/download/id
    [HttpGet]
    [Route("api/v1/documents/download/{id}")]
    public HttpResponseMessage GetFile(int id){}

    // triggers with GET /api/v1/documents/id
    [HttpGet]
    [Route("api/v1/documents/{id}")]
    public HttpResponseMessage GetFileInfo(int id){}
 }

Untested, but it should work