I have a login page. In my web.config I setup a loginUrl so that if a user tries to go to an “Authorized” page and are not authorized they will get redirected to the login page.
Now I noticed when this happens and the user gets redirected from a “Authorized” page the url from the page they are getting redirected from gets appended to the login url.
So that way when they do login I can use that I can send them back to the page they where trying to get too.
So this is how the Url would look:
http://localhost:2505/CMS_Account/LogOn?ReturnUrl=%2fCMS_Home%2fIndex
So I am trying to capture the ReturnUrl querystring part as a parameter in my View.
But I can’t get it took work.
So I found out if I change my Form for the login to this:
<% using (Html.BeginForm()) ........
Then I can capture the ReturnURl for some reason no problem.
However how I have it right now I have this:
<% using (Html.BeginForm("Login","Authentication",FormMethod.Post,new { id = "frm_Login"})) .....
Once I try to pass the parameters into the BeginForm it stops capturing the ReturnUrl.
I don’t know why it stops. Some people say that it is because I am using the default route and somehow if you don’t put anything in the beingForm it magically can figure out the ReturnUrl with the default url.
Soon as you put something in BeginForm it seems to get dumb and you need to give it a route to tell it what to do.
I don’t know how to write this route though. I tried quite a few different combinations and they all failed and everyone who tells me right a route never tell me how it should look like.
So I don’t know what to try anymore.
What I tried
routes.MapRoute(
"CMS_Account", // Route name
"CMS_Account/{action}/{ReturnUrl}", // URL with parameters
new { controller = "CMS_Account", action = "LogOn",} // Parameter defaults
);
routes.MapRoute(
"CMS_Account", // Route name
"CMS_Account/{action}/{ReturnUrl}", // URL with parameters
new { controller = "CMS_Account", action = "LogOn", ReturnUrl = ""} // Parameter defaults
);
routes.MapRoute(
"CMS_Account", // Route name
"{controller}/{action}/{ReturnUrl}", // URL with parameters
new { controller = "CMS_Account", action = "LogOn", ReturnUrl = ""} // Parameter defaults
);
routes.MapRoute(
"CMS_Account", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "CMS_Account", action = "LogOn", id = ""} // Parameter defaults
);
routes.MapRoute(
"CMS_Account", // Route name
"{controller}/{action}/", // URL with parameters
new { controller = "CMS_Account", action = "LogOn"} // Parameter defaults
);
You don’t need to change your routes. The cool thing with the routing engine is that if you add an additional route value that isn’t declared in the route itself, the routing engine will throw it on the end as a get variable.
E.g. Have you tried putting the ReturnUrl into BeginFrom?
Controller as Dennis suggested:
Then in your view you’ll need to use the BeginForm(string action, string controller, object routeValues, FormMethod method, object htmlAttributes) overload. E.g.
HTHs,
Charles
EDIT: Just on a side note, the other way around it would be to put the ReturnUrl into a hidden input field – after initial getting it from the querystring. This means the value is then in your post collection and you don’t have to worry about getting it back into your querystring.