Redirecting Response asynchronously in Custom HttpHandler ASP.Net MVC 5

256 Views Asked by At

When response is redirected through Task aync and await.Redirection cannot not be seen on client side(Browser). Browser is waiting for response always. Basically I created a custom HttpHandler which is furthur returning MVCHandler thus it is invoking

IAsyncResult BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, object state)

I overrided this method for redirection but redirection is going through another thread(or something).

protected override IAsyncResult BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, object state)
    {
        if (_hasPermission)
        {
            return base.BeginProcessRequest(httpContext, callback, state);
        }
        else
        {
            return DisplayError(httpContext.Response);
        }
    }

 public async Task DisplayError(HttpResponse _response)
    {
        Task.Run(() =>
        {
            _response.RedirectToRoute("Unauthorized");
        }).RunSynchronously();
    }

I also tried to make it synchronous by writing above and another method. But still the same problem.Is there any way to fix this Or preventing MVCHandler to invoke ProcessRequest instead of BeginProcessRequest method?

1

There are 1 best solutions below

1
On

The particular technology used to implement your HTTP server code (. NET/C#) in this case does not change the HTTP protocol.

You can change how you do stuff on the server (async-await), but you can't dasher the pipeline.