[英]Unable to fetch returnurl in asp.net mvc Controller
我有两个控制器Base和Login。
基础控制器:
public ActionResult start()
{
string action = Request.QueryString[WSFederationConstants.Parameters.Action];
}
登录控制器:
public ActionResult Login(string user,string password,string returnUrl)
{
if (FormsAuthentication.Authenticate(user, password))
{
if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null)
returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery);
return RedirectToAction("Start","Base", returnUrl });
}
return View();
}
身份验证完成后,它会按预期重定向到Base Controller中的Start操作。 但是查询字符串不能获取值。 当悬停在查询字符串上时,它显示长度值,但不显示uri。
如何使用从Base Controller中的Login控制器发送的url并从中获取参数?
您实际上是将302返回给客户端。 来自文档 。
返回对浏览器的HTTP 302响应,这会导致浏览器向指定的操作发出GET请求。
这样做时,客户端将使用您创建的URL发出另一个请求。 在你的情况下像youruri.org/Base/Start
。 查看浏览器中的网络选项卡(Chrome中的F12)。
我认为你想做的是:
return RedirectToAction
("Start", "Base", new { WSFederationConstants.Parameters.Action = returnUrl });
假设WSFederationConstants.Parameters.Action
是常量。 如果WSFederationConstants.Parameters.Action
返回字符串fooUrl
您的操作将向浏览器返回以下内容:
Location:/Base/Start?fooUrl=url
Status Code:302 Found
另一种选择是将值实际传递给控制器:
public class BaseController: Controller
{
public ActionResult start(string myAction)
{
string localAction = myAction; //myAction is automatically populated.
}
}
并在您的重定向:
return RedirectToAction
("Start", "Base", new { myAction = returnUrl });
然后BaseController
将自动获取参数,您无需从查询字符串中获取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.