user175084
user175084

Reputation: 4630

exception catching in Global.ascx app_error

I have web application in asp.net and C#

I am trying to handle exceptions if they occur anywhere within this application.

like suppose the behaviour should be if and exception like this occurs

//generate your fictional exception
            int x = 1;
            int y = 0;
            int z = x / y;

it should catch it in the app_error of the global.ascx file and redirect it to the Default.aspx page. i got the logging part but the redirect is not working as i still get the

Server Error in '/' Application. page. or may be it is redirecting and getting killed in the middle..

this is what is there in global.ascx

 protected void Application_Error(object sender, EventArgs e)
    {
        logger.Fatal(this.Server.GetLastError().GetBaseException());
        logger.Info("FatalLogger Passed");
        //get reference to the source of the exception chain
        Exception ex = Server.GetLastError().GetBaseException();
        Response.Redirect("~/Default.aspx?error=MessageHere");
    }

this in the code in web.config

<authentication mode="Forms">
        <forms loginUrl="Login.aspx" defaultUrl="~/Default.aspx" name="GUI" slidingExpiration="true" timeout="30" path="/">
        </forms>
    </authentication>

any ideas.. ill; be happy to provide more information.

Thanks

ok i want this approach for a reason because whenever there is an error the user get logged out and i dont want that to happen instead go to the default page

Upvotes: 0

Views: 384

Answers (3)

robaker
robaker

Reputation: 1029

Have you tried calling Server.ClearError() before the redirect in Application_Error? It's been a while since I played with this, but I believe that if you don't call ClearError then the framework still thinks the error is unhandled.

Upvotes: 1

Gavin Ward
Gavin Ward

Reputation: 1022

Try using Server.Transfer(page)

Also be wary of passing the error message via the Query String as it can open you up to XSS problems. Pass an error code and then display the message dependent on the code (using a switch statement)

Upvotes: 0

jeroenh
jeroenh

Reputation: 26782

Configure custom error pages

BTW, I recommend ELMAH for the logging part...

Upvotes: 1

Related Questions