当用户上传超过限制的文件时,我尝试显示错误页面(请参阅
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"); } }
解决方法
尝试这样:
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); }