Search code examples
asp.net-web-api2asp.net-web-api-routing

Web API One action works while a nearly identical one doesn't?


Error Message

{
    "Message": "No HTTP resource was found that matches the request URI 'https://localhost:44390/api/UserRoutes?effectiveDate=3/29/2019'.",
    "MessageDetail": "No type was found that matches the controller named 'UserRoutes'."
}

Working Action

public class AdvanceOrderApiController : BaseApiController
{
    [HttpGet, Route("api/AdvanceOrders")]
    public AdvanceOrdersResult GetAdvanceOrdersForRouteDate(string route, DateTime effectiveDate)
    {
        ...
    }
}

// JavaScript Usage: route="0100" and effectiveDate="03/29/2019".
API.SendRequest("/api/AdvanceOrders", "GET", { route: route, effectiveDate: effectiveDate }, success, failure);

Not Working Action

public class UserApiController : BaseApiController
{
    [HttpGet, Route("api/UserRoutes")]
    public IEnumerable<string> GetUserRoutes(DateTime effectiveDate)
    {
        ...
    }
}

// JavaScript Usage: effectiveDate="03/29/2019"
API.SendRequest("/api/UserRoutes", "GET", { effectiveDate: effectiveDate }, success, failure);

WebApiConfig

Not sure that it's relevant since I'm just declaring the route for each action, but...

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        ...

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

API.SendRequest

This function is just a wrapper around jQuery's $.ajax function, nothing fancy. If the code is necessary I'll present it, but it works for all my other API calls so I can't imagine it would be the source of the problem.

These actions are nearly identical, why does one work and the other doesn't?


Solution

  • Passing the date in as Igor said in the comments presented an error message that revealed that I had an Api controller in my Permissions area that had a route also named api/UserRoutes.

    Once I changed the name of the route the problem resolved. I just wish it could have just told me this error message from the start.