I have set up a default route in my .NET MVC 5 app like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
So now whenever I access my site at:
localhost:59920
It opens me up the index controller from home.
Now I have more methods inside the same controller like:
Register, Login and such
How can I set up a route so that it looks like this:
localhost:59920/Login
localhost:59920/Register
Using Attribute routing
public class AccountController : Controller
{
[Route("Login")]
public ActionResult Login()
{
return View();
}
}
With Traditional routing
routes.MapRoute(
name: "Login",
url: "Login",
defaults: new { controller = "Account", action = "Login" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Now when you request yourSite/Login
, the Login action method in AccountController will handle that.