Posted By: zhuo | Nov 9th, 2005 @ 7:46 PM
page 1 of 1
Comments: 6 | Views: 5164
Hi all,

I've implemented the Application_Error handler in Global.asax.cs which handles all unhandled errors thrown by the ASP.NET application.

This error handler is fine for most situations but it seems to handle HTTP errors as well, e.g. 404 errors. This didn't really come as a surprise to me but my question is how do I selectively handle certain errors in Application_Error and leave the rest rethrown again.

E.g. I don't want to handle HTTP 404 errors, I want to redirect it to a error page for HTTP 404 based on web.config. But for other errors I want Application_Error to handle it.

The alternatively solution I am looking for is a way of detecting that an HTTP error has occured and redirect it programmatically along with its status code.

Bonus for me if you have solution to both alternatives.

Thank you
James Smiley
quangcaosanpham
quangcaosanpham
http://www.tt24g.com
Hi all,

I write :

protected void Application_Error(Object sender, EventArgs e)
        {

            if (Server.GetLastError().InnerException != null)
            {
                Exception ex = Server.GetLastError().GetBaseException();
                string strSubject = "Indochina Error: " + ex.Message;
                string strBody = String.Empty;
                strBody += "<p>URL: " + HttpContext.Current.Request.Url.ToString();
                strBody += "<p><font color=red>" + ex.ToString().Replace("\n", "<br>").Replace("   at ", "&nbsp;&nbsp;&nbsp;at ") + "</font>";
                strBody += "<p>User IP: " + HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] + " &nbsp; " + HttpContext.Current.Request.ServerVariables["REMOTE_HOST"];
                strBody += "<p>Date/Time: " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
                strBody += "<p>User Agent: " + HttpContext.Current.Request.UserAgent;
                if (HttpContext.Current.Request.UrlReferrer != null)
                    strBody += "<p>Referrer: " + HttpContext.Current.Request.UrlReferrer.ToString();
                TC.Indo.CommonFuntion.SendMail_v2("admin@tt24g.com", "admin@tt24g.com", "", "", strSubject, strBody);
               
                if (Request.RawUrl.ToUpper() == "/DEFAULT.ASPX") //If Home page raise error
                {
                    Response.Redirect("/index.html");
                }
                else
                {
                    Response.Redirect("/");
                }
            }
        }

in page Globle.asax.cs


but not run.

Why, i not find error ???


Thank

Please post your question in its own thread rather than resurrecting an unrelated thread.

Your Application_Error method won't do anything if the exception has no inner exception.

To answer the original poster's question, check if the exception's type is HttpException. You can get the HTTP error code by calling the HttpException's GetHttpCode() method.

I don't think you can redirect from global.asax.  You need to enable custom errors in your web config and set the default redirect.

To answer the original post in custom errors tag in your web config you can handle specific errors by doing:

<customErrors mode="On" defaultRedirect="~/AppError.aspx">
            <error statusCode="404" redirect="AppError404.aspx"/>
</customErrors>


Yes you can redirect from global.asax, ie. Server.Redirect("Error.aspx");

page 1 of 1
Comments: 6 | Views: 5164
Microsoft Communities