-1

I have a common custom error page for my asp.net website because it's common it is shown on every error I want to found the last error code which was occurred and redirected to my that custom error page so that I can show right message according to the error which was occurred.

Note : solution have to be session based, I don't want any user to show error which was occurred on any other user's system of course.

yogi
  • 19,175
  • 13
  • 62
  • 92

3 Answers3

-1

What I would suggest is extending the UI.Page class and using that class for all your pages.

In that class (I know vb not c# but same principle and easy to convert) use the following code:

Public Class _PageBase
    Inherits System.Web.UI.Page

#Region "Page Functions"

    Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
        Session("error_StackTrace") = Server.GetLastError.StackTrace.ToString
        Session("error_Message") = Server.GetLastError.Message.ToString
        Session("error_Page") = Request.Url.ToString
        Session("error_Source") = Server.GetLastError.Source.ToString

        Server.ClearError()

        Response.Redirect("~/errors/Error.aspx")
    End Sub

#End Region

End Class

This will fire on all pages using that base class, and pass the last 'server' error (which will be the error the user caused), store all the details in session and pass it over to your error page. Then you can do as you wish.

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
  • Why you answer with a question ? – Aristos Sep 20 '12 at 08:57
  • I do not thing that downvote have to do with my question. Is not me anyway. – Aristos Sep 20 '12 at 09:05
  • The PageError is not good place to handle and capture this kind of errors. I do not know if the session can be saved at that point. I also find very risky to make here stuff like that because can lead to stack overflow if an error occur here. The only good use of the PageError is to close possible open handlers and free memory. – Aristos Sep 20 '12 at 09:09
  • The session can be dealt with fine here, I've implemented it on many sites and it works fine. – Ryan McDonough Sep 20 '12 at 09:46
-1

Do you use IIS or Apache?

For Apache Configuring Apache to serve customized error pages is extremely easy; there is a section of the httpd.conf file devoted to this. It takes just one directive per error to achieve. If you open the conf file and scroll right down to almost the very bottom of section two, you’ll see the section you need to edit. By default, these directives are commented out, but all you need to do is un-comment each directive and then change the path to point to your own error page.

ErrorDocument 404 /errordocs/404error.html

For IIS IIS 6: Edit Website or virtual Directory then Userdefinded Error. There you can edit all error files and change to a user defined asp.net file.

IIS 7: Detailed Error Message see: http://blogs.msdn.com/b/rakkimk/archive/2007/05/25/iis7-how-to-enable-the-detailed-error-messages-for-the-website-while-browsed-from-for-the-client-browsers.aspx

Steven Spyrka
  • 678
  • 7
  • 18
-1

Not good idea what you try to do. You must capture the errors on the code that they occur inside the page, and show the message on that page - stay on page - and if this is possible give the user the opportunity to correct it. If your error gets out of your try/catch and out of control then log it and fix it.

The only error that you can show to your user is the "non found page".

You can get the last error as Exception LastOneError = Server.GetLastError();

And there you can read more about errors: How do I make a "generic error" page in my ASP.NET application so that it handles errors triggered when serving that page itself?

and How to tell if an error captured by my global.asax was displayed on the screen of the user

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150