Implementing TransferResult in MVC 3 RC - does not work
Asked Answered
S

2

5

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.

Symonds answered 13/11, 2010 at 20:59 Comment(2)
did you ever fix this problem?Coneflower
yep, here is the working solution: #800011Symonds
P
1

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();
    }
}
Periwig answered 14/11, 2010 at 23:0 Comment(1)
This code works properly if using Visual Studio Devt Server for debugging. However I do experience the above error when running the app on the local IIS server. I did not have a chance yet to test the app on a remote server, but I clearly observed the difference in behavior in these two configurations on the local machine.Symonds
R
0

Personally I think that creating routes (with route constraints if necessary (see http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx) is far preferable to this 'hack' which attempts to perform an invisible redirect so that the request is handled by a different controller and action than the one specified by routing.

Why can't you just use routing?

Rani answered 14/11, 2010 at 0:20 Comment(1)
I use TransferResult for implementing error handling: the current page action or view are being processed, and if something happens, a transfer is executed.Symonds

© 2022 - 2024 — McMap. All rights reserved.