Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
    }
}
like image 806
user603007 Avatar asked Jul 15 '11 06:07

user603007


People also ask

Is global asax mandatory in MVC?

We know that Global. asax is an optional file to handle Application level event in ASP.NET application.

How does redirect to action work?

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.

Is global asax mandatory?

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).


1 Answers

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);
}
like image 123
Darin Dimitrov Avatar answered Nov 21 '22 22:11

Darin Dimitrov