开发者

Custom 404 error not redirecting properly

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@email.co.uk"

     Errormail.From         = "servererror@email.co.uk"
     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 err开发者_JAVA技巧or

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜