我可以从global.asax重定向到控制器操作吗?

use*_*007 14 c#-4.0 asp.net-mvc-2

我试图在用户上传超过限制的文件时显示错误页面(请参阅捕获"超出最大请求长度")

在global.asax我想重定向到一个控制器动作,所以这样的东西,但它不起作用?:

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

Dar*_*rov 24

试试这样:

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);
}
Run Code Online (Sandbox Code Playgroud)