开发者

can i redirect from the global.asax to controller action?

i am trying to show an error page when the user uploads a file that is over the limit (see Catching "Maximum request 开发者_如何学运维length exceeded")

in the global.asax i want to redirect to a controller action, so something like thisbut it does not work ?:

private void Application_Error(object sender, EventArgs e)
{
    if (GlobalHelper.IsMaxRequestExceededEexception(this.Server.GetLastError()))
    {
        this.Server.ClearError();
        return RedirectToAction("Home","Errorpage");
    }
}


Try like this:

protected void Application_Error()
{
    var exception = Server.GetLastError();
    // TODO: Log the exception or something
    Response.Clear();
    Server.ClearError();

    var routeData = new RouteData();
    routeData.Values["controller"] = "Home";
    routeData.Values["action"] = "ErrorPage";
    Response.StatusCode = 500;
    IController controller = new HomeController();
    var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
    controller.Execute(rc);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜