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 ![]()
-
-
Hi,
read this.
C -
Wasn't what I wanted.
Anyone else??
-
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 ", " at ") + "</font>";
strBody += "<p>User IP: " + HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] + " " + 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>
-
scott976 said: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");
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.