Question

I have ASP.NET MVC application. I want my application to redirect from

example.com/Register

to

example.com/Account/Register

How can I do it with routes? It makes little sense to me to make controller only for this one task

public class RegisterController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Register", "Account");
    }
}
Was it helpful?

Solution

You don't need a redirect. You need a custom route

Add this route first (above "Default")

routes.MapRoute(
                    "Register",
                    "Register",
                    new { controller = "Account", action = "Register" }
                );

This solution will leave the user on URL example.com/Register, but instantiate Controller Account, execute ActionResult Register, and return View Account/Register.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top