MVC3中登陆成功后返回原来的访问页面
比如某用户登陆系统后 进入某个画面操作
过一段时间再去操作时 用户过期 需要从新登陆了 这时登陆后怎么返回到刚才操作的页面?
思路是有个 但是怎么拿到刚才操作页面的地址 返回登陆后怎么返回到这个地址?
把当前页面的URL当成URL的参数
登陆成功后再返回这个参数
[解决办法]
用Request.ServerVariables["http_referer"]可以取URL。
[解决办法]
关键词:ReturnUrl
View:Logon.cshtml
@using (Html.BeginForm("LogOn", "Account", new { ReturnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post))
{...
Controller中的Logon Action:
[HttpPost]
public ActionResult Logon(LogonInfo model, string returnUrl)
{
if (ModelState.IsValid)
{
string[] loginResult;
if (!string.IsNullOrEmpty(model.UserID) && !string.IsNullOrEmpty(model.Password))
{
// 验证登陆
if (this.TryLogin(model.UserID, model.Password, out loginResult))
{
//model.UserName = string.Empty;
//Response.Cookies.Remove(model.UserID);
Response.Cookies.Remove(model.UserID);
// 记住登陆状态
FormsAuthentication.SetAuthCookie(model.UserID, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
}
return View(model);
}