【问题标题】:asp.net mvc simple questionasp.net mvc 简单问题
【发布时间】:2010-07-27 07:52:36
【问题描述】:

我正在创建 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>

登录控制

<%@ 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)){ %>

【问题讨论】:

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


    【解决方案1】:

    由于您正在实现自己的登录机制,因此您需要确保在用户单击您的提交按钮时传递 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 参数。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 2011-10-28
      • 2014-06-25
      相关资源
      最近更新 更多