2010-10-22 83 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