Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing TransferResult in MVC 3 RC - does not work

Any ideas how to fix the below?

There is a great implementation of TransferResult available here, which worked great on MVC 1,2 but doesn't work on MVC 3 RC.

public class TransferResult : RedirectResult 
{ 
    public TransferResult(string url): base(url) 
    { 
    } 

    public override void ExecuteResult(ControllerContext context) 
    { 
        var httpContext = HttpContext.Current; 
        httpContext.RewritePath(Url, false); 
        IHttpHandler httpHandler = new MvcHttpHandler(); 
        httpHandler.ProcessRequest(HttpContext.Current); 
    } 
} 

On MVC 3 RC, httpHandler.ProcessRequest fails and says 'HttpContext.SetSessionStateBehavior' can only be invoked before 'HttpApplication.AcquireRequestState' event is raised.

How to rewrite this code to make this work?

UPD: The code works if run on VS 2010 built-in development server, but fails to run on IIS 7.5 localhost. The problem is still unresolved.

UPD2 This answer contains a modified implementation of TransferResult which works with MVC3. Turns out it is even simpler than it used to be.

like image 212
Andy Avatar asked Nov 15 '22 05:11

Andy


1 Answers

Unable to reproduce. The following works perfectly fine in MVC 3 RC (Razor and WebForms):

public class TransferResult : RedirectResult
{
    public TransferResult(string url)
        : base(url)
    {
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var httpContext = HttpContext.Current;
        httpContext.RewritePath(Url, false);
        IHttpHandler httpHandler = new MvcHttpHandler();
        httpHandler.ProcessRequest(HttpContext.Current);
    }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return new TransferResult("/Home/About");
    }

    public ActionResult About()
    {
        return View();
    }
}
like image 169
Darin Dimitrov Avatar answered Dec 30 '22 00:12

Darin Dimitrov