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

Web API Routing {controller}/{action}/{id} vs default routing


I want to create a controller that can respond to those requests:

  1. api/User/123
  2. api/User/UsersOfCompany?id=123&pageIndex=0&pageSize=20
  3. api/User/AllowedCompanies?id=123&pageIndex=0&pageSize=10

This is my actual code.

User Controller

public class UserController : ApiController
{
    [HttpGet]
    public IHttpActionResult Get(int id)
    {
        //... implementation...
    }

    [HttpGet]
    [ActionName("UsersOfCompany")]
    public IHttpActionResult UsersOfCompany(int id, [FromUri] Paging paging)
    {
        //... implementation...
    }

    [HttpGet]
    [ActionName("AllowedCompanies")]
    public IHttpActionResult AllowedCompanies(int id, [FromUri] Paging paging)
    {
        //... implementation...
    }
}

WebApiConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Servizi e configurazione dell'API Web

        // Route dell'API Web
        config.MapHttpAttributeRoutes();

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

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

        config.MessageHandlers.Add(new LanguageMessageHandler());
    }
}

RouteConfig

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Based on sample request above, I would like routing work this way:

  1. Get(int id)
  2. UsersOfCompany(int id, [FromUri] Paging paging)
  3. AllowedCompanies(int id, [FromUri] Paging paging)

But, now I can only call the last 2 requests (UsersOfCompany, AllowedCompanies). When I call api/User/123 I get this error:

"Could not find an HTTP resource that matches the request's URL 'Service/api/User/123'.", "Unable to find an action on the 'User' controller with name '42887'."

I already tried to invert the order of route registration but I otained the opposite behavior, so that api/User/123 call works and the others two not.

Where's the problem?


Solution

  • Your error message clearly states that WebApi tries to match Service/api/User/123 to the first pattern: api/{controller}/{action}/{id}.

    You could try to change order of registration in your WebApiConfig or to specify the route explicitly:

    [Route("api/User/{id:int}")]
    [HttpGet]
    public IHttpActionResult Get(int id)
    {
        //... implementation...
    }