2010-10-22 82 views
6
页面后重定向

让我们以一个例子Visual Studio创建一个标准的默认的ASP.NET MVC项目。ASP.NET MVC登录用户来自

正在登录../Home/About页面我点击登录链接并进入../Account/LogOn页面。在我登录后我被重定向到Home页,不给Home/About页面从中我得到的LogOn页。 这是为什么?

虽然,在我们的AccountController有:

[HttpPost] 
    public ActionResult LogOn(LogOnModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      if (MembershipService.ValidateUser(model.UserName, model.Password)) 
      { 
       FormsService.SignIn(model.UserName, model.RememberMe); 
       if (!String.IsNullOrEmpty(returnUrl)) 
       { 
        return Redirect(returnUrl); 
       } 
       else 
       { 
        return RedirectToAction("Index", "Home"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

是一个事实,即string returnUrl是点击登录链接后空的问题? 如何在点击登录链接时为其赋值?

如何重定向用户,登录,页面,从他/她来自后?

查看代码:

<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server"> 
<h2>Log On</h2> 
<p> 
    Please enter your username and password. <%: Html.ActionLink("Register", "Register") %> if you don't have an account. 
</p> 

<% using (Html.BeginForm()) { %> 
    <%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %> 
    <div> 
     <fieldset> 
      <legend>Account Information</legend> 

      <div class="editor-label"> 
       <%: Html.LabelFor(m => m.UserName) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(m => m.UserName) %> 
       <%: Html.ValidationMessageFor(m => m.UserName) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(m => m.Password) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.PasswordFor(m => m.Password) %> 
       <%: Html.ValidationMessageFor(m => m.Password) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.CheckBoxFor(m => m.RememberMe) %> 
       <%: Html.LabelFor(m => m.RememberMe) %> 
      </div> 

      <p> 
       <input type="submit" value="Log On" /> 
      </p> 
     </fieldset> 
    </div> 
<% } %> 

编辑(添加):

看来,上述观点的代码是不是问题非常相关的,而不是我应该看着这个(LogOnUserControl.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
<% 
    if (Request.IsAuthenticated) { 
%> 
    Welcome <b><%: Page.User.Identity.Name %></b>! 
    [ <%: Html.ActionLink("Log Off", "LogOff", "Account") %> ] 
<% 
} 
else { 
%> 
    [ <%: Html.ActionLink("Log On", "LogOn", "Account")%> ] 
<% 
} 
%> 
+0

我们能否看到您的观点?和你的web.config? – Gregoire 2010-10-22 08:19:06

回答

14

当您生成登录链接,您需要通过returnUrl查询字符串参数:

<%: Html.ActionLink("Log On", "LogOn", "Account", new { returnUrl = Request.Url }, null)%> 
+0

是的,这种方式完美的工作。谢谢。 +1 – rem 2010-10-22 08:43:22

+2

较新的ASP.NET MVC模板正在使用'RedirectToLocal'。所以你应该使用'Request.Url.PathAndQuery'根据[this](http://stackoverflow.com/questions/27560840/redirecttolocal-goes-to-a-different-url) – roland 2015-11-06 16:29:02

+0

这工作@rolan – usman 2016-10-28 10:10:58

2

为什么这很容易回答。

ReturnURL没有设置,因为你logOn自己通过点击登录

如果一个页面有限制,无论是角色还是用户,你会得到自动重定向到登录的动作,以及ReturnUrl将被设置。

http://localhost:50152/Account/LogOn?ReturnUrl=%2f2011-2012%2fReeksen 

可以做的是更改角落中的Log On按钮,以便使用ReturnURL。

+0

感谢您的有用信息,+1 – rem 2010-10-22 08:52:20

2

提交一个表单,表单没有对RETURNURL的值,所以我不惊讶你的returnurl为空。

这里有您需要在客户端做什么。

- 地方

<input type='hidden' value=<%myurl%> name='returnurl' /> 

然后你使用模型绑定您的表单中添加此...我不使用模型绑定,因此我不确定是否将returnurl正确绑定到除了模型之外声明的额外参数。

如果没有,那么这样做你的服务器端代码中

myurl = Request.Form["returnurl"] 

应该为你现在的工作。

+0

Cyril,I以另一种方式解决这个问题。无论如何,感谢您的提议。这将有助于我更好地理解这个问题。 +1 – rem 2010-10-22 08:55:01