Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error using Global.asax asp.net

Tags:

c#

asp.net

I made an error; the page directs users in case of error in any applications within the website. I made Global.asax rather than using Webconfig. My question is : Is it possible to redirect user from Global.asax for those statusCodes "401", "404" and "500" in case of error rather than using Webconfig ?

In other words, using Global.aspx rather than Webconfig !? I am just curious to know.

Thank you


1 Answers

    protected void Application_Error(Object sender, EventArgs e)
    {
         Exception ex = this.Server.GetLastError();

         if(ex is HttpException)
         {
              HttpException httpEx = (HttpException)ex;

              if(httpEx.GetHttpCode() == 401)
              {
                   Response.Redirect("YourPage.aspx");
              }
         }
    }

Yes it is possible. Here is little code example. This should be added in Global.asax.cs.

like image 115
mybirthname Avatar answered Dec 13 '25 08:12

mybirthname



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!