How do I execute a controller action from an HttpModule in ASP.NET MVC?

后端 未结 4 1237
遥遥无期
遥遥无期 2021-01-03 16:20

I\'ve got the following IHttpModule and I\'m trying to figure out how to execute an action from a controller for a given absolute or relative URL.



        
4条回答
  •  没有蜡笔的小新
    2021-01-03 17:05

    Hi use this to let the framework execute the code for that path using the routing and all the components :

        // MVC 3 running on IIS 7+
        if (HttpRuntime.UsingIntegratedPipeline)
        {
            context.Server.TransferRequest(url, true);
        }
        else
        {
            // Pre MVC 3
            context.RewritePath(url, false);
    
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(httpContext);
        }
    

    And ideally the request processing is completer at this point. If this is not the case and if the request is further processed along the asp.net http pipeline, then use this to stop the request at this point, and tell asp.net that we're done with this request :

    HttpApplication app = (HttpApplication) context.Application;
    app.CompleteRequest();;
    

    Im not sure if the context has the Application (im not near VS now) but use it to stop the request in this module if needed.

提交回复
热议问题