Search code examples
c#asp.net-web-apiasp.net-web-api2routeattribute

Route is not found with empty RoutePrefix on controller


I have someones code here and I do not understand it above all the "empty" RoutePrefix on the controller.

I call the url in the browser like this:

http://localhost/TestService/TestAccess/FindProducts/de/2/product/5

I get a 404 for this controller + action:

[RoutePrefix("")]
public class TestAccessController : ApiController
{

    [Route("{country}/{brandlist}/product/{databaseID:int}")]
    [HttpGet]
    public async Task<IHttpActionResult> FindProducts(String country, String brandlist, int databaseID)
    {
        ...
    }
}

That is the route setup:

 config.MapHttpAttributeRoutes();
 config.Routes.MapHttpRoute(name: "Default", routeTemplate: "{controller}" );

What route do I have to enter in the browser url bar to trigger my FindProducts endpoint without changing the existing code?


Solution

  • The route attribute is absolute - it doesn't take the controller name unless you specify it in the route (or set a route prefix at the controller level).

    Try changing your route to

    [Route("TestAccess/FindProducts/{country}/{brandlist}/product/{databaseID:int}")]
    

    Edit

    Or try to change the url to...

    http://localhost/de/2/product/5
    

    or possibly http://localhost/TestService/de/2/product/5