Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Redirecting errors to 500.aspx page in asp.net2.0

Posted on 2011-09-14
5
403 Views
Last Modified: 2012-05-12
Hi,

I am working on asp.net2.0 application using C#  

I wanted to redirect all my ineternal server error pages to 500.aspx page and there it should print log with all details

How should I do it?

Please guide

Thanks
0
Comment
Question by:tia_kamakshi
5 Comments
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36540869
Use <customErrors> web-config element for redirection in case of specific or any error.

For logging you can use Application_Error event of HttpApplication class in Global.asax.

See this link
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36540874
0
 
LVL 3

Accepted Solution

by:
chandrasekar1 earned 250 total points
ID: 36541213
Hi Tia,

above experts are right !!

You need to handle the internal server errors through page or application level, the better way you can go with Application level event in Global.asax

protected void Application_Error(Object sender, EventArgs e)
{
  // At this point we have information about the error
  HttpContext ctx = HttpContext.Current;

  Exception exception = ctx.Server.GetLastError ();

  string errorInfo =
     "<br>Offending URL: " + ctx.Request.Url.ToString () +
     "<br>Source: " + exception.Source +
     "<br>Message: " + exception.Message +
     "<br>Stack trace: " + exception.StackTrace;

//Create a separate method “Exceptionlogging” which logs the     //exception through enterprise library or write the error info in a XML

  Exceptionlogging(errorInfo);
 
  // --------------------------------------------------
  // To let the page finish running we clear the error
  // --------------------------------------------------
  ctx.Server.ClearError ();
}

Then to redirect the error page 500.aspx, you need to set Custom error in web.config, Even you can set the more specific error redirection, for 404 error it will go to Pagenotfound.aspx

<customErrors mode="On" defaultRedirect="~/errors/500.aspx">
     <error statusCode="404" redirect="~/errors/PageNotFound.aspx" />
</customErrors>

0
 

Author Comment

by:tia_kamakshi
ID: 36541269
Hi,

In web.config I have written

<customErrors mode="On" defaultRedirect="~/errors/500.aspx">
                  <error statusCode="404" redirect="~/errors/PageNotFound.aspx" />
                  <error statusCode="500" redirect="~/errors/500.aspx" />
            </customErrors>
            
            
            <customErrors mode="On" defaultRedirect="~/errors/500.aspx">
                              <error statusCode="404" redirect="~/errors/PageNotFound.aspx" />
                              <error statusCode="500" redirect="~/errors/500.aspx" />
            </customErrors>
            
            
and in global.asax file i have written below code:

void Application_Error(object sender, EventArgs e)
    {        
       
        HttpContext ctx = HttpContext.Current;

        Exception exception = ctx.Server.GetLastError();
        Logger.Error(String.Format("{0}: errorInfo:{1}", ctx.Request.Url.ToString(), exception));
        ctx.Server.ClearError();          
       
    }
   
   
    But it is not forwarding the 500 errors to 500.aspx file
   

Please guide

Many Thanks
0
 
LVL 8

Assisted Solution

by:jagrut_patel
jagrut_patel earned 250 total points
ID: 36541377
Can you try after commenting,

ctx.Server.ClearError();          
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question