2010-07-27 52 views
0

我正在創建asp.net mvc登錄頁面。這很簡單。與其他人一樣。控制器包含2個方法。asp.net mvc簡單問題

我的問題是

我調試首先LogOn方法。 ReturnUrl有價值。例如"Admin/Index"。調試完第二個LogOn方法後。但是ReturnUrl是空的。

public ActionResult LogOn(string ReturnUrl) // First method 
{ 
    return View(); 
} 

[HttpPost]   
public ActionResult LogOn(string eUserName, string ePassword, Boolean rememberMe, string ReturnUrl) //Second Method 
{ 
... 
} 

我的看法是這裏>>

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title>Login</title>  
</head> 
<body> 
<table style="width: 100%"> 
      <tr> 
       <td align="center"> 
        <div style="width: 800px;"> 
         <%=Html.ValidationSummary() %> 
        </div> 
       </td> 
      </tr> 
      <tr> 
       <td align="center"> 
       <% Html.RenderPartial("LoginControl"); %> 
       </td> 
      </tr> 
    </table> 
</body> 
</html> 

Logincontrol

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
<div style="text-align: left; width:150px;margin-top:20px;"> 
    <% using (Html.BeginForm("Logon", "Account", FormMethod.Post)) 
     { %> 

    <div> 
     Хэрэглэгчийн нэр:</div> 
    <div> 
     <%= Html.TextBox("eUserName") %> 
    </div> 
    <div style="margin-top:5px;"> 
     Нууц үг:</div> 
    <div > 
     <%= Html.Password("ePassword") %> 
    </div> 
    <div style="margin-top:5px;"> 
     <%= Html.CheckBox("rememberMe") %> 
     <label class="inline" for="rememberMe"> 
      Намайг сана?</label> 
    </div> 
    <div style="text-align:center;margin-top:5px;"> 
     <input type="submit" value="Нэвтрэх" /> 
    </div> 


    <%} %> 
</div> 

爲什麼RETURNURL返回null?

發生了什麼?

編輯

感謝蒂姆·羅伯茨

最後正確的代碼是在這裏>>

<% using (Html.BeginForm("Logon", "Account",new { ReturnUrl = HttpContext.Current.Request.QueryString["returnUrl"]} FormMethod.Post)){ %> 

回答

3

既然你實現自己的登錄機制,你需要確保returnUrl參數在用戶點擊提交按鈕時傳遞。這樣做的一個方法是使用BeginForm方法的重載:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
<div style="text-align: left; width:150px;margin-top:20px;"> 
    <% using (Html.BeginForm("Logon", "Account", new { ReturnUrl = HttpContext.Current.Request.RawUrl }, FormMethod.Post)) 
     { %> 
     ... as before 
    <% } %> 
</div> 

哪些應該通過URL一起在查詢字符串,然後將其綁定到動作方法的參數RETURNURL。希望這可以幫助。