Created
June 27, 2013 12:20
-
-
Save duncansmart/5875998 to your computer and use it in GitHub Desktop.
MVC way to do Server.Transfer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace Foo.Bar | |
{ | |
// Adpated from http://stackoverflow.com/questions/799511/how-to-simulate-server-transfer-in-asp-net-mvc | |
public class TransferToRouteResult : ActionResult | |
{ | |
public string RouteName { get; set; } | |
public RouteValueDictionary RouteValues { get; set; } | |
public TransferToRouteResult(RouteValueDictionary routeValues) | |
: this(null, routeValues) | |
{ | |
} | |
public TransferToRouteResult(string routeName, RouteValueDictionary routeValues) | |
{ | |
this.RouteName = routeName ?? string.Empty; | |
this.RouteValues = routeValues ?? new RouteValueDictionary(); | |
} | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
if (context == null) | |
throw new ArgumentNullException("context"); | |
var urlHelper = new UrlHelper(context.RequestContext); | |
var url = urlHelper.RouteUrl(this.RouteName, this.RouteValues); | |
context.HttpContext.Server.TransferRequest(url, true); | |
} | |
} | |
public static class ControllerExtensions | |
{ | |
public static TransferToRouteResult TransferToAction(this Controller controller, ActionResult result) | |
{ | |
return new TransferToRouteResult(result.GetRouteValueDictionary()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment