0

I'm currently trying to change a few URL's for Google Analytics, as I want the URL in the google sites to be easier to read.

Right now, My login page is at the root, and the dashboard page is also at root. However, I want my login page to display as www.somesite.com/login, when the user types in www.somesite.com. As of right now, this login page exist at accounts/login, which would display www.somesite.com/accounts/login.

Is it possible for me to edit the RouteConfig file to display /login when the controller accounts and action login is hit? Currently, this does not work:

routes.MapRoute(
    name: "Default",
    url: "Login/{controller}/{action}/{id}",
    defaults: new
    {
        controller = "Accounts",
        action = "Login",
        id = UrlParameter.Optional
    },
    namespaces: new[] { "somenamespace" }
);

The action being called:

    public ActionResult Logon()
    {
        return View(ViewModel.CreateModelForPageLoad<LogOn>());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Logon(Logon model)
    {
         //Log in code
     }

3 Answers 3

2

I'm not able to test this right now but this should work:

    routes.MapRoute(
        name: "Login",
        url: "login",
        defaults: new
        {
            controller = "Accounts",
            action = "Logon"
        }
    );
Sign up to request clarification or add additional context in comments.

6 Comments

Just gives me a 404... hmm
What does the action signature look like? i.e does it have any parameters?
If you copied my answer verbatim then it will have failed, I did not know the action was named Logon, I had put Login. I updated my answer to reflect the proper names/spellings.
No I made the proper changes, maybe this has something to do with the MVC?
It shouldn't. I use this exact route on one of my apps: codeshare.io/2BANBL
|
0
        routes.MapRoute(
        name: "Default",
        url: "Login",
        defaults: new
        {
            controller = "Accounts",
            action = "Login",
            id = UrlParameter.Optional
        },
        namespaces: new[] { "somenamespace" }
    );

The easier way is to do attribute routing then you would just need this

public class Accounts : Controller{
    [HttpPost, Route("login")]
    public ActionResult Login(int id)
    {....}
}

The above of course works in MVC 5 as attribute routing was added starting MVC 5.

1 Comment

hmm, I'm getting a 404.
0

public class HomeController : Controller {

    public ActionResult Login()
    {
        return View();
    }

    public ActionResult Dashboard()
    {
        return View();
    }

}

public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Login",
            url: "login/{action}/{id}",
            defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
        );

        routes.MapRoute(
           name: "Dashboard",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
       );
    }
}

1 Comment

I tested it and it is working perfectly for your given scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.