I have some actions:
public partial class MyController : Controller
{
public ActionResult Action1()
{
}
public ActionResult Action2(int id)
{
}
public ActionResult Action3(string id)
{
}
public ActionResult Action4(string name)
{
}
}
Do I need register routes for each action like this:
routes.MapRoute("r1", "{controller}/{action}/{id}", new { id = UrlParameter.Optional });
routes.MapRoute("r2", "{controller}/{action}/{name}", new { name = UrlParameter.Optional });
Or there is some way to register one pattern route for all actions or maybe I need some kind of “hack”?
Similar urls should use similar routes. So in this case you have only a single url pattern which is
/controller/action/someid. So simply use the default route:and then update your actions:
As far as the last action is concerned the
nameparameter could be passed as query string. If you really insist on it being part of the path, you could rename it toid. It is better to pass arbitrary strings such as names as query string parameters and not as part of the url paths.