1

Is there a way to include two Get methods with different names but without any input parameters in a single controller in ASP.NET MVC 4 Web API?

For example, I want to have two GetBookXXX methods – one returning a list with only the book titles and the other returning a list with all book details (title, author, ISBN, etc). The approach I have seen so far is to use two separate controllers, but I would prefer to have a single one and not duplicating the plumbing code twice.

Thanks,

quarkx
  • 29
  • 2
  • Have you given it a try to see if it caused any issues? – CD Smith Jun 10 '12 at 01:06
  • [This](http://stackoverflow.com/questions/9499794/single-controller-with-multiple-get-methods-in-asp-net-web-api?rq=1) looks like the answer your looking for: – Steve Aug 11 '12 at 15:37

1 Answers1

0

Yes, there is. Define your methods as follows:

[ActionName("GetBookXXX")]
public ActionResult BookTitles()
{
    return View();   
}

[ActionName("GetBookXXX")]
public ActionResult BookDetails(int id)
{
    return View();   
}

See http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx for further reference.

berezovskyi
  • 3,133
  • 2
  • 25
  • 30