rem*_*rem 6 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);
}
Run Code Online (Sandbox Code Playgroud)
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>
<% } %>
Run Code Online (Sandbox Code Playgroud)
编辑(补充):
看来上面的视图代码与问题不太相关,相反我应该看看这个(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")%> ]
<%
}
%>
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 14
生成LogOn链接时,需要传递returnUrl查询字符串参数:
<%: Html.ActionLink("Log On", "LogOn", "Account", new { returnUrl = Request.Url }, null)%>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24890 次 |
| 最近记录: |