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");
}
}
We know that Global. asax is an optional file to handle Application level event in ASP.NET application.
The RedirectToAction() Method This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response.
asax is not required by ASP.NET for a website to run. It is, however, very useful for application-level functionality (like unhandled exception logging).
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With