5

Is it "legal" to have a controller inherit a route from its BaseController ? It seems it's not allowed for Attribute Routing , but how about normal route registration via RouteCollection?

The reason is I currently have a bunch of controllers, each representing some kind of file converter. Each of them has a common set of methods to upload the file to be converted. These method are endpoints on each controller not just private methods. I'd like for the following routes to be valid:

/api/controller1/uploadfile
/api/controller2/uploadfile
/api/controller3/uploadfile

Can I get an example how this could be done inside a BaseController and if it's not possible, an alternative.

Community
  • 1
  • 1
parliament
  • 21,544
  • 38
  • 148
  • 238

3 Answers3

2

Here's what works:

public abstract class BaseUploaderController : ApiController
{
    [HttpGet, Route("uploadfile")] //Needs both because HttpGet("uploadfile") currently only supported in MVC attribute routing 
    public string UploadFile() 
    {
        return "UploadFile";
    }
}


[RoutePrefix("api/values")]
public class ValuesController : BaseUploaderController
{
    [Route("{id:int}")]
    public string Get(int id)
    {
        return "value";
    }
}
parliament
  • 21,544
  • 38
  • 148
  • 238
  • 1
    yeah that may very well be the case sorry. See if AttributeRouting provides a solution. http://attributerouting.net/#asp-net-mvc – parliament Feb 21 '14 at 20:32
  • I assume you set up the imperative routing? Those route attributes make it seem like you got attribute routing to work, which it doesn't work for me. – Robert C. Barth Mar 13 '14 at 09:57
1

Are you looking to place this UploadFile action in the base controller and other controllers inheriting from them should still be able to hit UploadFile from their respective routes like you mentioned in your post? If yes, you could create an abstract base api controller and place this UploadFile action in it and your requests to the individual controllers should work as expected.

Example:

public abstract class BaseApiController : ApiController
{
    // POST /api/Values
    // POST /api/Test
    public string UploadFile()
    {
        return "UploadFile";
    }
}

public class TestController : BaseApiController
{
    // GET /api/test/10
    public string GetSingle(int id)
    {
        return "Test.GetSingle";
    }
}

public class ValuesController : BaseApiController
{
    // GET /api/values/10
    public string GetSingle(int id)
    {
        return "Values.GetSingle";
    }
}
Kiran
  • 56,921
  • 15
  • 176
  • 161
  • Hey sorry this either doesn't work or my question was unclear. With the setup you have I want `api/values/uploadfile` and `api/test/uploadfile` to both be valid routes and hit UploadFile method. Currently it thinks "uploadfile" is an integer I'm passing to api/value/{int} route and that throws a conversion error. I need it to hit the base class instead – parliament Dec 03 '13 at 19:13
  • ok..i just was trying to give an example...but let's say if you have a route like `api/{controller}/{id}, id = RouteParameter.Optional`, then a request like `POST /api/test/uploadfile` would indeed hit the base UploadFile method...could you share how your routes are setup? – Kiran Dec 03 '13 at 20:07
  • Thank you for the example. That's the route I have and it doesnt work. Gives the error I described in the comment. I used your example in a brand new project – parliament Dec 03 '13 at 21:24
  • I concur, using a base class doesn't work. I don't think there's a way to use a base class. Which sucks. – Robert C. Barth Mar 13 '14 at 09:47
1

As per this answer https://stackoverflow.com/a/21610390/122507 attribute routes are not inherited.

I am currently debating between introducing unnecessary method in 30 controllers just so I can add an attribute route or add a fake parameter to the base class method to let the default routing disambiguate between Get(int id) and GetHistory(int id, bool history) where I don't need the second parameter.

Community
  • 1
  • 1
Stilgar
  • 22,354
  • 14
  • 64
  • 101