View Full Version : Custom Errors
seppey
07-04-2003, 01:13 AM
Does anyone know if there is anyway to get custom error pages to work?
I have tried creating a .htaccess file which points to my custom 404 error page (admin/error.aspx) but if I point to a file that is not there, then the normal windows 404 error appears.
Can custom errors be aspx files?
[?]
Humanitad.org
pjoyce
07-04-2003, 03:14 AM
yes, custom error pages are easy to set up in .NET and you can use .ASPX pages. All you need to do is edit your web.config file a little bit. The setup is very flexible, you can have a page to catch specific errors, such as 404 errors, as well as a default error page.
In your web.config file look for the <customErrors/> tag. By default I believe it is set to "RemoteOnly," meaning that detailed .NET error messages will be shown in the page only if you are actually on the server machine (not too useful in a hosted scenario.) If your 404 page is "My404.aspx," in your web root then your would change this to read:
[quote]<customErrors</fontid="maroon">mode</fontid="red">="On"</fontid="blue">>
<error</fontid="maroon">statusCode</fontid="red">="404"</fontid="blue">redirect</fontid="red">="/My404.aspx"</fontid="blue">/>
</customErrors>
</CODE>
If you want a "generic" error page, you add the defaultRedirect attribute to the <customErrors> element, thus:
[quote]<customErrors</fontid="maroon">mode</fontid="red">="On"</fontid="blue">defaultRedirect</fontid="red">="/MyOtherErrors.aspx"</fontid="blue">>
<error</fontid="maroon">statusCode</fontid="red">="404"</fontid="blue">redirect</fontid="red">="/My404.aspx"</fontid="blue">/>
</customErrors>
</CODE>
CAUTIONS: The web.config is an XML file, so it is case sensitive. CustomError is not equal to customError. Also, when you create a new web app, the customErrors element will be closed... that is it will contain a "/". Make sure to get rid of that and close the tag with the closing </customErrors</font id="maroon">>.
Last word on Errors -- the Global.asax contains a stub for the Application_Error event. This event is raised whenever an error occurs in your application. You can use this to do your own error logging to a datbase or whatever. I have mine set up to e-mail me, if you'd like the code for that, let me know.
bdwest
07-11-2003, 08:39 AM
I only seem to be able to get the custom error sot work with aspx page request. For example if you click on the following url should get a custom error.
www.sanjuanflows.info/oops.aspx
However, I dont get a custom error with
www.sanjuanflows.info/oops
I don't understand why. Here is the section of my webconfig.
<customErrors defaultRedirect="Error.aspx" mode="RemoteOnly" >
<error statusCode="404" redirect="Error.aspx?errCode=404" />
<error statusCode="500" redirect="Error.aspx?errCode=500" />
</customErrors>
I have an error page that accepts parameters so I can generate different messages on the same page.
Any ideas?
Thanks
Brian
[b]quote:Originally posted by pjoyce
yes, custom error pages are easy to set up in .NET and you can use .ASPX pages. All you need to do is edit your web.config file a little bit. The setup is very flexible, you can have a page to catch specific errors, such as 404 errors, as well as a default error page.
In your web.config file look for the <customErrors/> tag. By default I believe it is set to "RemoteOnly," meaning that detailed .NET error messages will be shown in the page only if you are actually on the server machine (not too useful in a hosted scenario.) If your 404 page is "My404.aspx," in your web root then your would change this to read:
[quote]<customErrors</fontid="maroon">mode</fontid="red">="On"</fontid="blue">>
<error</fontid="maroon">statusCode</fontid="red">="404"</fontid="blue">redirect</fontid="red">="/My404.aspx"</fontid="blue">/>
</customErrors>
</CODE>
If you want a "generic" error page, you add the defaultRedirect attribute to the <customErrors> element, thus:
[quote]<customErrors</fontid="maroon">mode</fontid="red">="On"</fontid="blue">defaultRedirect</fontid="red">="/MyOtherErrors.aspx"</fontid="blue">>
<error</fontid="maroon">statusCode</fontid="red">="404"</fontid="blue">redirect</fontid="red">="/My404.aspx"</fontid="blue">/>
</customErrors>
</CODE>
CAUTIONS: The web.config is an XML file, so it is case sensitive. CustomError is not equal to customError. Also, when you create a new web app, the customErrors element will be closed... that is it will contain a "/". Make sure to get rid of that and close the tag with the closing </customErrors</font id="maroon">>.
Last word on Errors -- the Global.asax contains a stub for the Application_Error event. This event is raised whenever an error occurs in your application. You can use this to do your own error logging to a datbase or whatever. I have mine set up to e-mail me, if you'd like the code for that, let me know.
</blockquote id="quote"></font id="quote">
Yes, this method will only work for .aspx pages. We are working on a tool where you can specify the customer error pages for other extensions.
DiscountASP.NET
bdwest
07-11-2003, 10:21 AM
Thanks for clearning this up - I thought I was doing something wrong.
Brian
[b]quote:Originally posted by dasp
Yes, this method will only work for .aspx pages. We are working on a tool where you can specify the customer error pages for other extensions.
DiscountASP.NET
</blockquote id="quote"></font id="quote">
pjoyce, I would appreciate if you could post the code you mentioned you have in your global file to email you any errors.
Thanks!
pjoyce
07-15-2003, 08:25 AM
Sure, I'll be glad too, but I'm a bit busy day and night until friday. Can you wait until this weekend?
[b]quote:Originally posted by arby
pjoyce, I would appreciate if you could post the code you mentioned you have in your global file to email you any errors.
Thanks!
</blockquote id="quote"></font id="quote">
Sure, no problem. Thanks!
[b]quote:Originally posted by pjoyce
Sure, I'll be glad too, but I'm a bit busy day and night until friday. Can you wait until this weekend?
[b]quote:Originally posted by arby
pjoyce, I would appreciate if you could post the code you mentioned you have in your global file to email you any errors.
Thanks!
</blockquote id="quote"></font id="quote">
</blockquote id="quote"></font id="quote">
pjoyce
07-16-2003, 11:03 AM
This is in C# but I'm guessing people can translate to VB.NET. If you make any improvements to the code, I'd appreciate it if you could let me know. There are a couple of things that are specific to my application, namely the e-mail address and configuration parameters. I have a configuration parameter to turn the e-mail of errors on or off and another for the e-mail address to send to. Both these are in the web.config file. Your global.asax should already have a method for Application_Error which gets called for all .ASPX errors. You will have to add the ParseException method.
[quote]
protectedvoidApplication_Error(Objectsender,EventA rgse){
System.Exceptionerr=Server.GetLastError();
try{;
if(PScheduler.Config.ApplicationSettings.MailError s){
System.Web.Mail.MailMessageMsg=newSystem.Web.Mail. MailMessage();
Msg.To=PScheduler.Config.ApplicationSettings.Error MailAddress;
Msg.From="Someone@MyDomain.com";
Msg.Body="ErrorinMyApplicationapplicationat\n"+System.DateTime.UtcNow.ToString()+"UTC";
Msg.Body+="Onserver"+Server.MachineName+"\n\n";
Msg.Body+=ParseException(err);
Msg.Subject=(err.GetBaseException()==null?"ErrorinMyApp":"ErrorinMyApp:"+err.GetBaseException().Message);
Msg.Subject+="AT"+System.DateTime.UtcNow.ToString()+"UTC";
System.Web.Mail.SmtpMail.Send(Msg);
}
}
catch(System.ExceptionErrorErr){
//Suppresserror--canthaveerrorsraisedhere.
}
}
privatestringParseException(System.Exceptionerr){
stringMessage=string.Empty;
try{
Message="Errorin"+err.Source+":"+err.Message+"\n";
Message+="StackTrace"+err.TargetSite.Name+"\n";
Message+=err.StackTrace+"\n";
if(err.InnerException!=null){
Message+="InnerException:\n";
Message+=ParseException(err.InnerException)+"\n";
}
}catch(System.Exceptione){
//Mustcatchexceptionsheretopreventendlessloop
Message="Errorparsingexception";
}
returnMessage;
}
</CODE>
Thanks, this worked great!
We launched an IIS Manager tool in the control panel where you can control your 404 and 500 error pages.
DiscountASP.NET
Malin
08-15-2004, 03:51 AM
Thanks for your code pjoyce
Is there a way to e-mail handled errors as well?
I tested your code and application_error in global.asax does not fire if there is an handled error - but still I want to know about these errors as well.
Malin
pjoyce
08-18-2004, 01:40 AM
[b]quote:Originally posted by Malin
Thanks for your code pjoyce
Is there a way to e-mail handled errors as well?
I tested your code and application_error in global.asax does not fire if there is an handled error - but still I want to know about these errors as well.
Malin
</blockquote id="quote"></font id="quote">
The application doesn't know if you handle an error, so there is no "universal" way of doing it. You would have to put the code to e-mail you the error in the catch statement.
Malin
08-18-2004, 02:58 AM
[b]quote:Originally posted by pjoyce
[b]quote:Originally posted by Malin
Thanks for your code pjoyce
Is there a way to e-mail handled errors as well?
I tested your code and application_error in global.asax does not fire if there is an handled error - but still I want to know about these errors as well.
Malin
</blockquote id="quote"></font id="quote">
The application doesn't know if you handle an error, so there is no "universal" way of doing it. You would have to put the code to e-mail you the error in the catch statement.
</blockquote id="quote"></font id="quote">
OK, then I will create a class for this where the constructor does the mailing and then make an instance of this class in every catch. This leaves med with just one line of code in every catch.. which I find acceptable.
Thanks for your answer
Malin
vBulletin® ©Jelsoft Enterprises Ltd.