Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Created June 27, 2013 12:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save duncansmart/5875998 to your computer and use it in GitHub Desktop.
Save duncansmart/5875998 to your computer and use it in GitHub Desktop.
MVC way to do Server.Transfer
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