Asp.net MVC遇到问题,将查询字符串作为参数传递

时间:2021-06-23 04:11:50

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.

我有一个登录页面。在我的web.config中,我设置了一个loginUrl,这样如果用户试图进入“授权”页面并且未经授权,他们将被重定向到登录页面。

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.

现在我注意到当这种情况发生并且用户从“授权”页面被重定向时,他们被重定向的页面中的URL被附加到登录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:

这就是Url看起来的样子:

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.

所以我试图将ReturnUrl查询字符串部分捕获为我的视图中的参数。

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.

然后我可以捕获ReturnURl由于某种原因没有问题。

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.

一旦我尝试将参数传递给BeginForm,它就会停止捕获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.

我不知道为什么会停止。有人说这是因为我正在使用默认路由,不知何故如果你没有在beingForm中放任何东西,它就会神奇地找出带有默认URL的ReturnUrl。

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.

很快你在BeginForm中放了一些东西,它似乎变得愚蠢,你需要给它一个路径来告诉它该做什么。

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

4 个解决方案

#1


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.

您无需更改路线。路由引擎很酷的一点是,如果添加一个未在路由本身中声明的其他路由值,路由引擎会将其作为get变量抛出到最后。

E.g. Have you tried putting the ReturnUrl into BeginFrom?

例如。您是否尝试将ReturnUrl放入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.

然后在您的视图中,您将需要使用BeginForm(字符串操作,字符串控制器,对象routeValues,FormMethod方法,对象htmlAttributes)重载。例如。

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.

编辑:只是旁注,另一种方法是将ReturnUrl放入隐藏的输入字段 - 初始从查询字符串获取后。这意味着该值将在您的帖子集合中,您不必担心将其恢复到您的查询字符串中。

#2


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:

获取ReturnURL参数(或任何其他查询字符串参数)的最简单方法是将其添加到LogOn操作,如下所示:

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

#3


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.

当您在“当前”状态下配置表单时,您强制它进行POST,但您想要的数据位于查询字符串上。

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

您可以使用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:

将ReturnURL添加为表单中的隐藏字段,并确保您要发布到的Logon方法:

  • Accepts ReturnURL as a parameter
  • 接受ReturnURL作为参数

  • Performs a Request.Form["ReturnURL"]
  • 执行Request.Form [“ReturnURL”]

to get the URL value from the HTML form.

从HTML表单中获取URL值。

#4


Have you tried to use the built in FormsAuthentications system?

您是否尝试使用内置的FormsAuthentications系统?

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:

我没有在MVC中检查这个,但它应该工作:

FormsAuthentication.RedirectFromLoginPage(LogInAsUserName.Text, False)

FormsAuthentications - 4guysfromrolla

FormsAuthentications - 4guysfromrolla

#1


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.

您无需更改路线。路由引擎很酷的一点是,如果添加一个未在路由本身中声明的其他路由值,路由引擎会将其作为get变量抛出到最后。

E.g. Have you tried putting the ReturnUrl into BeginFrom?

例如。您是否尝试将ReturnUrl放入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.

然后在您的视图中,您将需要使用BeginForm(字符串操作,字符串控制器,对象routeValues,FormMethod方法,对象htmlAttributes)重载。例如。

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.

编辑:只是旁注,另一种方法是将ReturnUrl放入隐藏的输入字段 - 初始从查询字符串获取后。这意味着该值将在您的帖子集合中,您不必担心将其恢复到您的查询字符串中。

#2


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:

获取ReturnURL参数(或任何其他查询字符串参数)的最简单方法是将其添加到LogOn操作,如下所示:

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

#3


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.

当您在“当前”状态下配置表单时,您强制它进行POST,但您想要的数据位于查询字符串上。

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

您可以使用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:

将ReturnURL添加为表单中的隐藏字段,并确保您要发布到的Logon方法:

  • Accepts ReturnURL as a parameter
  • 接受ReturnURL作为参数

  • Performs a Request.Form["ReturnURL"]
  • 执行Request.Form [“ReturnURL”]

to get the URL value from the HTML form.

从HTML表单中获取URL值。

#4


Have you tried to use the built in FormsAuthentications system?

您是否尝试使用内置的FormsAuthentications系统?

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:

我没有在MVC中检查这个,但它应该工作:

FormsAuthentication.RedirectFromLoginPage(LogInAsUserName.Text, False)

FormsAuthentications - 4guysfromrolla

FormsAuthentications - 4guysfromrolla