Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

Is it possible to handle content page errors in master page instead of writing try/catch block in every content page?


thanks
Posted
Comments
surendersinghprajapati 10-Oct-12 4:45am    
You can also create a base class where you can handle errors and it could then used on master pages and content pages. even it could be useful to set errors on master page (if wanna show error in a fix place.)

1 solution

Hi,

It is not possible to handle all the content page error in master page.

The flow of execution is first master page pageload occur then content page pageload occur and then any user control page load occur. Hence its not possible to handle all the error in master page.

The one solution you have for this.

You can handle error in global.asax file in Application_Error() method.

Below is sample code

C#
void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        Exception objErr = Server.GetLastError().GetBaseException();
        string err = "Error in: " + Request.Url.ToString() +
                          ". Error Message:" + objErr.Message.ToString();

    }


Hope this will help you.
 
Share this answer
 
v2
Comments
Chandrashekar SK 10-Oct-12 3:33am    
You can even use this to redirect to a custom error page.. Been using this..
surendersinghprajapati 10-Oct-12 4:45am    
You can also create a base class where you can handle errors and it could then used on master pages and content pages. even it could be useful to set errors on master page (if wanna show error in a fix place.)
PleaseHelpCP 10-Oct-12 4:58am    
thanks Mohd. Mukhtar. i have tried the same solution as yours, still its not working
Mohd. Mukhtar 10-Oct-12 5:01am    
Could you please copy your code how you handling error.
PleaseHelpCP 10-Oct-12 5:22am    
Exception ex = Server.GetLastError().GetBaseException();
string PageUrl = Request.Url.ToString();
ErrorFn.ShowErrorLog(ex.Message, ex.ToString(), Request.RawUrl.ToString());

i also have set to custom errors mode on and
<error statuscode="500" redirect="/ErrorPage.aspx"> in that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900