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);
}
精彩评论