Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

Error handling in asp.net site

Hi,

I have this site where I want to redirect my errors to specific pages and also mail the error to a specific mail when it occurs. In order to do so I first altered my web.config like this:
<system.web>
    <customErrors mode="Off" defaultRedirect="Customerrorpage.aspx">
      <error statusCode="404" redirect="Error404.aspx" />
    </customErrors>
</system.web>

Open in new window


Then I added the following code to Global.asax.
 Sub Application_Error(sender As Object, e As EventArgs)
        Dim objErr As Exception = Server.GetLastError().GetBaseException()
        Dim err As String = "<br><b>Error in: </b>" + Request.Url.ToString() + "<br><b>Error Message: </b>" + objErr.Message.ToString() + "<br><b>Stack Trace:</b><br>" + objErr.StackTrace.ToString()
        'Email sending method
        Dim msg As New EmailMessage

        'Dim path As String = "\\arkenmail\MailPickUp\"

        msg.Server = "smtp.server"
        msg.Username = "test@test.com"
        msg.Password = "psw"
        msg.From = "info@test.com"
        msg.FromName = "ErrorPost"
        msg.To = "mymail@test.com"
        msg.Subject = "Error in code"
        msg.Body = err
        msg.CharSet = "utf-8"
        msg.BodyFormat = aspNetEmail.MailFormat.Html

        If msg.Send Then

        End If
    End Sub

Open in new window

Unfortunately none of the scenarios kicks in. When I have a 404 for example it doesn't redirect to the specified page.

What am I missing?

Thanks for help!
Peter
SOLUTION
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peter Nordberg

ASKER

Hi and thanks for answer,

I've already tested that like so:
<customErrors mode="On" defaultRedirect="Customerrorpage.aspx">
      <error statusCode="404" redirect="Error404.aspx" />
    </customErrors>

Open in new window

But still get the usual error message:
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Peter
please check whether the custom error page is located in right folder or some other resource is not found
I've placed two the files in the root of the site.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial