I am using attribute routing with the following routes:
[RoutePrefix("api/books/{id}")]
public BooksController : ApiController
{
[Route("")]
public IHttpActionResult GetByTitle(int id,string title);
[Route("")]
public IHttpActionResult GetByTitleAuthorIsbn(int id,string title, string author, string isbn);
}
if the user goes to this route:
GET api/boos/1?title=abc&author=xyz
then web api will route this url to the default GET method i.e.
public IHttpActionResult GetByTitle(int id,string title);
What I would like to happen is that in the above mentioned scenario a 404 should be returned because the api doesn't have a route that accept title and author.
A couple of suggested solutions is to add the parameters in the route so
[RoutePrefix("api/books/{id}")]
public BooksController : ApiController
{
[Route("{title}")]
public IHttpActionResult GetByTitle(int id,string title);
[Route("{title}/{author/{isbn}}")]
public IHttpActionResult GetByTitleAuthorIsbn(int id,string title, string author, string isbn);
}
Another solution will be to add the method name in the route so
[RoutePrefix("api/books/{id}")]
public BooksController : ApiController
{
[Route("byTitle")]
public IHttpActionResult GetByTitle(int id,string title);
[Route("byTitleAuthorIsbn")]
public IHttpActionResult GetByTitleAuthorIsbn(int id,string title, string author, string isbn);
}
I would like to know if there is another solution as I am not a fan of "polluting" the route with extra route parameters or add method names in the route.
Please note that combining the two methods in one method is not the solution I am looking for. These 2 routes must stay 2 separate routes.
The example I show here is about books but it can be applied to anything so I said books just for argument sake.
Update:
This question is not a duplicate to this question as I have already included the answer in my question. I am looking for more alternatives.