asp.net mvc简单问题

发布于 2024-09-11 05:14:38 字数 2621 浏览 1 评论 0原文

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

我的问题是

我正在调试第一个 LogOn 方法。 ReturnUrl 具有价值。例如“管理/索引”。 debuggin 第二个 LogOn 方法之后。但 ReturnUrl 为 Null。

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

I'm creating asp.net mvc login page. This is simple. Same as others. Controller contains 2 method.

My problem is

I'm debugging First LogOn method. ReturnUrl has value. for example "Admin/Index". After debuggin Second LogOn method. But ReturnUrl is Null.

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

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

My View is here>>

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

Why ReturnUrl returns null?

What happening?

EDIT

Thanks Tim Roberts

Last correct code is here>>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2024-09-18 05:14:39

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

Since you're implementing your own login mechanism, you'll need to ensure that the returnUrl parameter is passed along when the user clicks on your submit button. One way of doing this is to use an overload of the BeginForm method:

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

Which should pass the URL along in the query string and then bind it to the action method's ReturnUrl parameter. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文