asp.net mvc简单的问题

时间:2010-07-27 07:52:37

标签: asp.net-mvc asp.net-mvc-2 loginview

我正在创建asp.net mvc登录页面。这很简单。和其他人一样。控制器包含2个方法。

我的问题是

我正在调试第一个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?

发生了什么?

修改

谢谢Tim Roberts

最后正确的代码在这里&gt;&gt;

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

1 个答案:

答案 0 :(得分: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,然后将其绑定到action方法的ReturnUrl参数。希望这会有所帮助。

相关问题