Attribute routing and inheritance

asp.net Web-Apiasp.net Web-Api-Routing

asp.net Web-Api Problem Overview


I am playing around with the idea of having a base controller that uses a generic repository to provide the basic CRUD methods for my API controllers so that I don't have to duplicate the same basic code in each new controller. But am running into problems with the routing attribute being recognized when it's in the base controller. To show exactly what the problem I'm having I've created a really simple WebAPI controller.

When I have a Get method in the main Controller and it inherits from the ApiController directly I don't have any problems and this works as expected.

[RoutePrefix("admin/test")]
public class TestController : ApiController
{
    [Route("{id:int:min(1)}")]
    public string Get(int id)
    {
        return "Success";
    }
}

When I move the Get method into a base controller it is returning the contents of the 404 page.

[RoutePrefix("admin/test")]
public class TestController : TestBaseController
{

}

public class TestBaseController : ApiController
{
    [Route("{id:int:min(1)}")]
    public string Get(int id)
    {
        return "Success";
    }
}

Some more interesting notes:

  • I can access the action at GET /Test/1. So it is finding it based on the default route still.

  • When I try to access POST /admin/test, it returns the following JSON

    { "Message":"No HTTP resource was found that matches the request URI 'http://test.com/admin/test'.";, "MessageDetail":"No type was found that matches the controller named 'admin'." }

Does anyone know of a way to get the routing to work with attributes from a base controller?

asp.net Web-Api Solutions


Solution 1 - asp.net Web-Api

Attribute routes cannot be inherited. This was a deliberate design decision. We didn't feel right and didn't see valid scenarios where it would make sense to inherit them.

Could you give a more realistic scenario as to where you would want to use this?

[Update(3/24/2014)]
In the upcoming 5.2 release of MVC Web API, there is going to be an extensibility point called System.Web.Http.Routing.IDirectRouteProvider through which you can enable the inheritance scenario that you are looking for here. You could try this yourself using the latest night builds(documentation on how to use night builds is here)

[Update(7/31/2014)]
Example of how this can be done in Web API 2.2 release:

config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());

//---------

public class CustomDirectRouteProvider : DefaultDirectRouteProvider
{
	protected override IReadOnlyList<IDirectRouteFactory> 
	GetActionRouteFactories(HttpActionDescriptor actionDescriptor)
	{
        // inherit route attributes decorated on base class controller's actions
		return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>
		(inherit: true);
	}
}

Solution 2 - asp.net Web-Api

Using Web API 2.2, you can:

public class BaseController : ApiController
{
	[Route("{id:int}")]
	public string Get(int id)
	{
		return "Success:" + id;
	}
}
[RoutePrefix("api/values")]
public class ValuesController : BaseController
{
}
   
config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());
public class CustomDirectRouteProvider : DefaultDirectRouteProvider
{
	protected override IReadOnlyList<IDirectRouteFactory> 
	GetActionRouteFactories(HttpActionDescriptor actionDescriptor)
	{
		return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>
		(inherit: true);
	}
}

as outlined here: http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-22

Solution 3 - asp.net Web-Api

Got it.

[Route("api/baseuploader/{action}")]
public abstract class BaseUploaderController : ApiController
{
    [HttpGet] 
    public string UploadFile() 
    {
        return "UploadFile";
    }
}


[Route("api/values/{action}")]
public class ValuesController : BaseUploaderController
{
    [HttpGet]
    public string Get(int id)
    {
        return "value";
    }
}

One caveat here is that the route action paramter must be the same as the action name. I could not find a way to get around that. (You cannot rename the route with a RouteAttribute)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeff TreutingView Question on Stackoverflow
Solution 1 - asp.net Web-ApiKiranView Answer on Stackoverflow
Solution 2 - asp.net Web-ApiDejanView Answer on Stackoverflow
Solution 3 - asp.net Web-ApiparliamentView Answer on Stackoverflow