HttpControllerSelector does not work with attribute routing

I am using a custom HTTP controller selector for my API version.

config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceSelector(config));

Below is my controller with actions:

[RoutePrefix("api/v1/messages")]
public class MessagesController : ApiController
{
    [Route("unreadall")] // api/v1/messages/unreadall
    public IEnumerable<long> UnreadAll()
    {
      // Return value;
    }
    [Route("{type}/unreadall")] // api/v1/messages/{type}/unreadall
    public IEnumerable<long> UnreadAll(string type)
    {
      // Return value;
    }
    [Route("unreadnext")] // api/v1/messages/unreadnext
    public long UnreadNext()
    {
      // Return value;
    }
    [Route("{type/}unreadnext")] // api/v1/messages/{type}/unreadnext
    public long UnreadNext(string type)
    {
      // Return value;
    }
    [Route("{id:long}/markasread")] // api/v1/messages/123/markasread
    [HttpPut]
    public string MarkAsRead(long id)
    {
        // Return value;
    }
    [Route("{id:long}")] // Default Action
    public string Get(long id) // api/v1/messages/123
    {
        // Return value;
    }
    [Route("")] // Default Action
    [HttpPost]
    public long Post(string message) // api/v1/messages
    {
        // Return value;
    }
}

Below is my route configuration:

config.Routes.MapHttpRoute(
                name: "DefaultApi1",
                routeTemplate: "api/{version}/{controller}/{id}/{action}"
            );

config.Routes.MapHttpRoute(
                name: "DefaultApi2",
                routeTemplate: "api/{version}/{controller}/{action}"
            );

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

When I test my routes, do the following work.

/api/v1/messages/unreadall
/api/v1/messages/unreadnext
/api/v1/messages/123/markasread

But the routes listed below also indicate the same actions.

/api/v1/messages/type/unreadall
/api/v1/messages/type/unreadnext

And I get errors for the rest of my routes.

/api/v1/messages/123
Error:
{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:59411/api/v1/messages/123'.",
    "MessageDetail": "No action was found on the controller 'MessagesController' that matches the name '123'."
}

POST: /api/v1/messages
Error:
{
    "Message": "The requested resource does not support http method 'POST'."
}

Can someone tell me what I am doing wrong with my route setup? or can someone send the working route configuration for my scripts above?

Appreciate your help!

Greetings

+4
source share
1 answer

: , , , .

, request.GetRouteData() . , , , , , .

, . , RouteCollectionRoute. , , . , , RouteData.Values:

var routeData = request.GetRouteData();
var subroutes = (IEnumerable<IHttpRouteData>)routeData.Values["MS_SubRoutes"];
var route = subroutes.First().Route;

: http://wildermuth.com/2013/11/12/Web_API_2_s_Attribute_Routing_Looking_Deeper

+2

All Articles