Search code examples
.netasp.net-mvcihttphandler

Redirect to controller action from custom IHttpHandler


I have an application in which users need to be able to upload large files (several gigabytes).

To allow for this, I have implemented a custom IHttpHandler and it seems to be working fine. However, I would like to redirect the user to a controller action once the custom IHttpHandler.ProcessRequest() has completed the file upload. Moreover, I need this redirection to happen in a way that will include all of the original request parameters.

This is my attempt:

    public void ProcessRequest(HttpContext context)
    {
        var isUploadReuqest = context.Request.Files.Count > 0;

        if (isUploadReuqest)
        {
            // snip

            context.Response.RedirectToRoute("Default", new { controller = "Home", action = "Upload" });
        }
    }

However, my attempt produces the following error:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Home/Upload

This is my RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{controller}/FileUpload");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Note that my customized HTTP handler is located at path */FileUpload. And yes, if you are wondering whether there is an action called Upload in the HomeController, there is :)

Thanks in advance for any replies!


Solution

  • After some further research I realized that I am attempting to redirect to a HttpPost action. Apparently that is not possible as a redirect by design is a GET request. I will need to think differently to solve my problem.