您可以使用请求查询字符串一直通过SignOn进程返回URL。
首先,指定页面返回时,你使您的点登录偏:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="Microsoft.Web.Mvc"%>
<!-- Your Example/Page1 page -->
<% if (!User.IsAuthenticated) %>
<%= Html.RenderAction("SignOn", "Account", new { returnUrl = Request.Url.PathAndQuery }); %>
使用如的RenderAction当前上下文是不是账户控制器。该功能目前不在MVC版本中,因此您需要在解决方案中包含ASP.NET MVC's Future library。
接下来,点登录控制器:
public ActionResult SignOn(string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
User user = userRepository.GetItem(u => u.aspnet_UserName == User.Identity.Name);
return !string.IsNullOrEmpty(returnUrl)
? Redirect(returnUrl)
: (ActionResult) RedirectToAction("Index", "Home");
}
return PartialView();
}
点登录形式:
<% using (Html.BeginForm("SignOn", "Account", new { returnUrl = Request.QueryString["returnUrl"] },FormMethod.Post,new {@class="my_signOn_class"}))
{ %>
<!-- Form -->
<% } %>
最后,在你点登录控制器处理表单POST,您可以将用户返回到 'RETURNURL' 使用以下代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SignOn(FormCollection formCollection, string returnUrl)
{
if (BusinessRuleViolated(formCollection))
{
if(!string.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home");
}
// SignIn(...)
}