chobo2
chobo2

Reputation: 85715

Having trouble with Asp.net MVC passing in a querystring as a parameter

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
);

Upvotes: 2

Views: 4245

Answers (4)

ten5peed
ten5peed

Reputation: 15890

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:

public ActionResult LogOn(string ReturnURL) {
    ViewData["ReturnURL"] = ReturnURL;
    return View();
}

Then in your view you'll need to use the BeginForm(string action, string controller, object routeValues, FormMethod method, object htmlAttributes) overload. E.g.

Html.BeginForm("Login",
               "Authentication",
               new { @returnUrl = ViewData["ReturnUrl"] },
               FormMethod.Post,
               new { @id = "frm_Login" })

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.

Upvotes: 6

Mathias F
Mathias F

Reputation: 15891

Have you tried to use the built in FormsAuthentications system?

If you can use this, you dont have to implement the functionality you describe.

I did not check this in MVC but it should work:

FormsAuthentication.RedirectFromLoginPage(LogInAsUserName.Text, False)

FormsAuthentications - 4guysfromrolla

Upvotes: 0

CoderDennis
CoderDennis

Reputation: 13837

You don't need to alter your routes.

The easiest way to get the ReturnURL parameter (or any other query string parameter) is to add it to your LogOn action like so:

public ActionResult LogOn(string ReturnURL) {
    ViewData["ReturnURL"] = ReturnURL;
    return View();
}

Upvotes: 3

Jeff Fritz
Jeff Fritz

Reputation: 9861

When you are configuring the form in your 'current' state, you are forcing it to POST, but the data you want is on the querystring.

You could access it with ControllerContext.HttpContext.Request.Querystring["ReturnURL"]

or...

Add the ReturnURL as a hidden field in the form, and ensure that your Logon method that you will post to either:

  • Accepts ReturnURL as a parameter
  • Performs a Request.Form["ReturnURL"]

to get the URL value from the HTML form.

Upvotes: 1

Related Questions