代码之家  ›  专栏  ›  技术社区  ›  ebattulga

asp.net mvc简单问题

  •  0
  • ebattulga  · 技术社区  · 15 年前

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

    我先调试 LogOn 方法。 ReturnUrl 有价值。例如 "Admin/Index" 方法。但是 为空。

    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)){ %>
    
    1 回复  |  直到 12 年前
        1
  •  3
  •   Tim Roberts    15 年前

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