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

后端 未结 4 1240
遥遥无期
遥遥无期 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 16:47

    Something along the lines should do the job:

    public void OnError(HttpContextBase context)
    {
        context.ClearError();
        context.Response.StatusCode = 404;
    
        var rd = new RouteData();
        rd.Values["controller"] = "error";
        rd.Values["action"] = "notfound";
        IController controller = new ErrorController();
        var rc = new RequestContext(context, rd);
        controller.Execute(rc);
    }
    

    You might also find the following related answer useful.

提交回复
热议问题