views:

42

answers:

1

I have this custom error redirect in my web.config file but it doesn't seem to be working since i added the redirectMode="ResponseRewrite"

It works fine for 500 errors but not for 404 (it just doesn't redirect when i have a 404)

Here's the code from web.config

<customErrors mode="On" redirectMode="ResponseRewrite"> 
   <error statusCode="404" redirect="/servererror/default.aspx" /> 
   <error statusCode="500" redirect="/servererror/default.aspx" /> 
</customErrors>

And here's my servererror/default.aspx code

Dim err As System.Exception = Server.GetLastError()

Dim Errormail = New MailMessage

     'Send email to Bondholder using email address from form
     Errormail.To           = "[email protected]"

     Errormail.From         = "[email protected]"
     Errormail.Subject      = "Server Error Alert"
     Errormail.BodyFormat   = MailFormat.Text
     Errormail.Priority     = MailPriority.Normal
     Errormail.Body         = ("Error on page - " & err.InnerException.Message & vbcrlf & vbcrlf & "URL of the page - " & Request.Url.ToString())

     SmtpMail.SmtpServer    = "localhost"

     SmtpMail.Send(Errormail)

I need to keep the redirectMode="ResponseRewrite" so that the servererror/default.aspx sends me an e-mail when there's an error

Any help would be much appreciated

Thanks

Jamie

UPDATE

I've had a look around the web and found quite a few other people having the same issues but I can't find a definitive answer.

Any ideas

Thanks

Jamie

+1  A: 

I assume you are seeing the generic .NET when the page cannot be found?

The solution is as per below. Note the addition of the ~ for the path:

    <customErrors mode="On" redirectMode="ResponseRewrite">
        <error statusCode="404" redirect="~/servererror/default.aspx"/>
        <error statusCode="500" redirect="~/servererror/default.aspx"/>
    </customErrors>

UPDATE:

Whilst your solution works, it is a little hacky - in particular the way you have made use of session state would be frowned upon in a large scale application or amongst other more seasoned developers. I suggest having the 500 and 404 errors split out onto different pages.

If your website is available via the web it is useful to display different error messages to real users i.e. if the page they are looking for has gone (404) or there are some temporary problems (500) and it may be a good idea to check back later. The text on error each page should reflect this accordingly. It is also important to report to the search engines appropriate status codes to indicate whether a page is no longer available i.e. it has been removed from your project and should be dropped from the search engine index or if there is an error and the search engine should return to crawl the page later and not drop from the index. Not reporting these status codes correctly may impair the performance of your web app via the search engines. Typically my web.config reads as below:

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/GenericError.aspx">
    <error statusCode="404" redirect="~/NotFound.aspx"/>
</customErrors>

Note: it is important to have the "defaultRedirect" attribute set due to a recently discovered security hole within the .NET Framework (ask Google). Setting this is a workaround to plug the hole.

To report proper HTTP status codes back to the client (and search engine bot) you can use the below code. Simply place them in the page load event of the relevant page:

For 500 errors:

Response.Status = "500 Internal Server Error"

For 404 errors:

Response.Status = "404 Not Found"

You can now remove the code from the Global.asax and create a shared function for your error email logic that can be called from each page. If you prefer you can merge the above code into your existing error page, however I always prefer to split them out as I find it neater.

If you want to test the HTTP status codes of your pages I recommend taking a look at a free application called Fiddler

BradB
Hi I tried that but still gave me the generic page cannot be found error
Jamie Taylor
That definitely works for me. By the way, is your app web facing i.e. you want it available via the search engines?
BradB
Hi yea it is available via search engines please see my answer I posted which seemed to solve my issue
Jamie Taylor
@Jamie - I've just updated my original answer. May be of use plus I eloborate on why I asked if your application is web facing and available via search engines.
BradB
I have now figured a way of sending the e-mail within the Global.asax file and my pages now re-direct as they should without using redirectMode="ResponseRewrite" as no matter which way i try that seems to break it
Jamie Taylor