Thodor12
Thodor12

Reputation: 149

ASP.NET Redirecting through specific mapped route depending on parameters

I am trying to make a login section. When you first launch the page you just go to the specified url /Login. When you submit and the credentials are wrong I want to redirect to the login page with the given url /Login/Error/{errorid}. So the idea is whenever the errorid parameter is added to a route it should enter the Error route. Otherwise take the default route.

However I have set up the following routes but they don't seem to work. Note: I did change the Default controller a little bit.

routes.MapRoute("Error", "{action}/Error/{errorid}",
    new { controller = "Login", action = "Login" }
);

routes.MapRoute("Default", "{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Furthermore I have 2 controllers. My Home controller is where the landing page is aswell as the Login controller.

I also get a error when launching the application. When it goes through the redirect in the Index action it says:

No route in the route table matches the supplied values.

HomeController:

public ActionResult Index()
{
    Session["User"] = -1;

    return RedirectToAction("Login", "Login");
}

LoginController:

public ActionResult Login(int? errorid)
{
    if (Models.User.CheckActiveSession(Convert.ToInt32(Session["User"])))
    {
        return RedirectToAction("Overview", "Home");
    }

    return View();
}

public ActionResult ValidateLogin()
{
    if (Request.HttpMethod == "POST")
    {
        int userid = Models.User.CheckLogin(Request.Form["username"], Request.Form["password"]);

        if (userid != -1)
        {
            Session["User"] = userid;

            return RedirectToAction("Overview", "Home");
        }
    }

    return RedirectToAction("Login", "Login", new { errorid = 1 });
}

I do have to say that I am new to this so I'm not sure if I'm doing this correct at all but I have read some guides on how you can do this. Any tips are appreciated. And if there are other ways you could this I would love to hear those aswell.

EDIT:

Got a small mistake within the custom route. The errorid parameter is not in the object of default parameters. If I do say that errorid = 0 then the page does load but I see the url /Login/Error.

Upvotes: 1

Views: 1384

Answers (1)

NightOwl888
NightOwl888

Reputation: 56849

The problem is with your default route:

routes.MapRoute("Default", "{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Although you have specified to provide a default controller value of "Home" if no value is provided in the URL, you have not included a way to provide {controller} in the URL. This means there is no way to specify anything for controller other than "Home". In other words, there is no way to get to your LoginController when using this default route.

There are many ways you could fix this (including going back to the original URL of {controller}/{action}/{id}), but it all depends on the reason why you decided to remove controller from the URL in the first place.

Got a small mistake within the custom route. The errorid parameter is not in the object of default parameters. If I do say that errorid = 0 then the page does load but I see the url /Login/Error.

This is not an error. If you want to make a required value in the URL, you must not provide a default value for it. Otherwise it will be optional (meaning you can leave it off of the right side of the URL).

For it to generate a URL matching your Error route, you need to provide a value for errorid (since it is not optional).

return RedirectToAction("Login", "Login", new { errorid = 123 });

Upvotes: 1

Related Questions