我试图在用户上传超出限制的文件时显示错误页面(请参阅Catching "Maximum request length exceeded")
在global.asax中的我想重定向到一个控制器动作,所以这样的东西但它不起作用?:
private void Application_Error(object sender, EventArgs e)
{
if (GlobalHelper.IsMaxRequestExceededEexception(this.Server.GetLastError()))
{
this.Server.ClearError();
return RedirectToAction("Home","Errorpage");
}
}
答案 0 :(得分:23)
试试这样:
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);
}