You could bring on the return URL all the way through the SignOn process, using the request query string.
First, specify the page to return to whenever you render your SignOn partial:
<%@ 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 }); %>
Use RenderAction if the current context is not the Account Controller. The feature is currently not in the MVC release, so you need to include the ASP.NET MVC's Future library in your solution.
Next, the SignOn controller:
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();
}
SignOn form:
<% using (Html.BeginForm("SignOn", "Account", new { returnUrl = Request.QueryString["returnUrl"] },FormMethod.Post,new {@class="my_signOn_class"}))
{ %>
<!-- Form -->
<% } %>
Finally, in your SignOn controller that processes the Form POST, you can return the user to the 'returnURL' using the following code:
[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(...)
}