登录到用户来自的页面后,ASP.NET MVC重定向

时间:2010-10-22 08:17:20

标签: c# asp.net-mvc redirect

我们以Visual Studio创建的标准默认ASP.NET MVC项目为例。

../Home/About页面上,我点击登录链接,然后转到../Account/LogOn页面。登录后,我将被重定向到Home页面,而不是Home/About页面,我将从该页面转到LogOn页面。 为什么?

尽管如此,在AccountController中我们有:

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.UserName, model.Password))
            {
                FormsService.SignIn(model.UserName, model.RememberMe);
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

点击登录链接后问题是string returnUrl为空吗? 单击登录链接时如何为其分配值?

如何在用户登录后将用户重定向到他/她来自的页面?

查看代码:

<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>Log On</h2>
<p>
    Please enter your username and password. <%: Html.ActionLink("Register", "Register") %> if you don't have an account.
</p>

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
    <div>
        <fieldset>
            <legend>Account Information</legend>

            <div class="editor-label">
                <%: Html.LabelFor(m => m.UserName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(m => m.UserName) %>
                <%: Html.ValidationMessageFor(m => m.UserName) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(m => m.Password) %>
            </div>
            <div class="editor-field">
                <%: Html.PasswordFor(m => m.Password) %>
                <%: Html.ValidationMessageFor(m => m.Password) %>
            </div>

            <div class="editor-label">
                <%: Html.CheckBoxFor(m => m.RememberMe) %>
                <%: Html.LabelFor(m => m.RememberMe) %>
            </div>

            <p>
                <input type="submit" value="Log On" />
            </p>
        </fieldset>
    </div>
<% } %>

已编辑(已添加):

上面的视图代码似乎与问题无关,相反我应该看一下( LogOnUserControl.ascx ):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (Request.IsAuthenticated) {
%>
    Welcome <b><%: Page.User.Identity.Name %></b>!
    [ <%: Html.ActionLink("Log Off", "LogOff", "Account") %> ]
<%
}
else {
%> 
    [ <%: Html.ActionLink("Log On", "LogOn", "Account")%> ]
<%
}
%>

3 个答案:

答案 0 :(得分:14)

生成LogOn链接时,需要传递returnUrl查询字符串参数:

<%: Html.ActionLink("Log On", "LogOn", "Account", new { returnUrl = Request.Url }, null)%>

答案 1 :(得分:2)

为什么这很容易回答。

ReturnURL未设置,因为您单独登录logOn

如果某个页面对角色或用户有限制,您将自动重定向到logOn操作,并且将设置ReturnUrl。

http://localhost:50152/Account/LogOn?ReturnUrl=%2f2011-2012%2fReeksen

可以做的是更改角落中的“登录”按钮以使用它返回ReturnURL。

答案 2 :(得分:2)

您正在提交表单,而且您的表单没有返回值,因此我对您的returnurl为空并不感到惊讶。

以下是您在客户端需要做的事情。

- 在您的表单中添加此内容

<input type='hidden' value=<%myurl%> name='returnurl' />

然后你正在使用模型绑定...我没有使用模型绑定,所以我不确定returnurl是否会正确绑定到除模型之外已声明的额外参数。

如果没有,请在服务器端代码

中执行此操作
myurl = Request.Form["returnurl"] 

现在应该适合你。