0

We're starting to migrate our flagship application from ASP.NET Webforms to ASP.NET MVC. Since it's pretty much a rewrite, we're trying to get as many good new features in as possible. One of these is the newfangled async. We're trying to make all the controllers and actions async (where it makes sense, of course) in hopes to get a better performance.

However the problem I've run into is that Response.Redirect and even Server.Transfer methods simply hang the request. It looks like something deadlocks somewhere.

This is a pity, because we're pretty used to the Response.Redirect method, which aborts everything you're doing and just redirects the entire request. Pretty handy, at least in webforms (makes the POST-REDIRECT-GET pattern easy to use).

Also, and perhaps more importantly, Response.Redirect and Server.Transfer is used in our Application_Error handler, which transfers the request to an error page. I do not even know of any alternatives that could be used there.

So... is there no way how we can bring Response.Redirect back to life?

Vilx-
  • 104,512
  • 87
  • 279
  • 422
  • @Richard - That question is about ASP.NET Webforms. This one is about MVC. Also, in that question it was suggested to basically abandon `Response.Redirect` by passing `false` as the second parameter (doesn't abort the request, just sets the redirect headers). Here I'm asking for a way to make it work even with `true` as the second parameter. So I really don't think they're duplicates. – Vilx- Sep 01 '14 at 12:47
  • Remember the redirect APIs are the same across WebForms and MVC... Plus the linked article shows what do to in the error case to avoid aborting threads (expensive) that using the compatibility version of redirect. – Richard Sep 01 '14 at 13:39
  • @Richard - Yes, but that approach involves passing whatever appropriate return codes. I might as well use `RedirectResult` then. Instead I want to see, if I can somehow keep the ability to perform a redirect-and-abort-request from any place in my code. Both me and my fellow programmers are very used to this practice. – Vilx- Sep 01 '14 at 15:02

1 Answers1

0

You may want to change your action return types to HttpResponseMessage (if not already) so you can have greater control over what is returned to the browser.

public Task<HttpResponseMessage> Action()
{
    return Task.Factory.StartNew(() =>
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Redirect);
        response.Headers.Location = new Uri("http://stackoverflow.com");
        return response;
    });
}

The above code will asynchronously handle a api action returning a redirect code and header to redirect the browser.

Phil Cooper
  • 3,083
  • 39
  • 63