The situation is, I need to use the same method for two action names in a ASP.NET controller. To elaborate, say http://mydomain/Members/Index
and http://mydomain/Members/Browse
should act as same or, have the same method as below for only Members controller:
public ActionResult Browse()
{
var members = _dbContext.Members.ToList();
return View("Browse", members);
}
Currently, I am using two ActionResults named "Browse" and "Index" with the same code but may be there is a better way to do this. I looked into Attributes but ActionName Attribute doesn't help me with what I need. Also, google searches give me suggestions with same methods but different HTTP request types and method overloading examples. Any help is very much appreciated.
I've used Index action in the controller and added a exclusive route for "Members/Browse" to point to the "Index" action of "Members" Controller. because in the other way around, "Members/" gives 404 error.
Here is the controller action now (example):
public ActionResult Index()
{
var members = _dbContext.Members.ToList();
return View("Index", members);
}
Routes defined in RouteConfig now:
routes.MapRoute(
name: "MemberBrowseRoute",
url: "Members/Browse",
defaults: new { Controller = "Members", Action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);