Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get clean ASP.Net error pages?

Tags:

asp.net

iis

xhtml

When I get an application error with an ASP.Net(3.5) site on IIS(6.0), I get dirty HTML code.

I would like to have auto generated XHTML code instead, it would be nice for web services which parse answer... (I am not interested by writing custom pages).

Is there a way to specify it ?

EDIT : EXAMPLE

Here is for example, the generated page for a 404 error, I would like clean XHTML code instead but I do not want to write it myself, can ASP.Net module do it?

<html>
    <head>
        <title>La ressource est introuvable.</title>
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Lucida Console";font-size: .9em}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
        </style>
    </head>

    <body bgcolor="white">

            <span><H1>Erreur du serveur dans l'application '/</b>/MyVirtualDirectory'.<hr width=100% size=1 color=silver></H1>

            <h2> <i>La ressource est introuvable.</i> </h2></span>

            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

            <b> Description : </b>HTTP 404. ...
            <br><br>

            <b> URL demandée: </b>/MyVirtualDirectory/MyService.aspx<br><br>

            <hr width=100% size=1 color=silver>

            <b>Informations sur la version :</b>&nbsp;Version Microsoft .NET Framework :2.0.50727.3053; Version ASP.NET :2.0.50727.3634

            </font>

    </body>
</html>

EDIT

I guess, the above HTML was generated by an ASPX page, is there a place where I can find it? To get inpired and translate it in XHTML then I will use @Remko 3rd solution to specify a custom error page.

like image 249
sinsedrix Avatar asked Dec 10 '12 10:12

sinsedrix


People also ask

Which is the best way to handle errors in net?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.

How do I disable .NET error pages?

Double-Click the "Error Pages" Icon in the middle window pane. Click on the "Edit Feature Settings..." link in the right window pane. Here you can specify either "Custom error pages", "Detailed errors", or "Detailed errors for local requests and custom error pages for remote requests" (Default). Hope this helps!

What causes server error in '/' application?

The “Server error in '/' application” can occur if there is a typo in the file extension, for example a file or URL that references test. htl instead of test. html. If the file name is correct, then you may need to add the MIME typeto the server.

How do I turn off custom error mode?

Click the Custom Errors tab. Select Off for custom error mode. Or navigate to the folder containing your application and open the web. config file in a text editor and edit by hand, and change the custom errors tag to <customErrors mode="Off" />.


2 Answers

There several ways to handle errors in ASP.NET and to control the contents of your response. Note: All methods require you to write your own custom XHTML code (despite your remark that that you're not interested in writing custom pages)

  1. At page level: Add a Page_Error event handler to your page.

    public void Page_Error(object sender,EventArgs e)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
        Response.Write("<p>Your perfectly formatted XHTML code.</p>");
        Server.ClearError();
    }
    

    If you call Server.ClearError() the error will not be handled by ASP.NET and you won't get the default error page.

  2. At application level: Add a Application_Error event handler to the Global.asax.

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
        Response.Write("<p>Your perfectly formatted XHTML code.</p>");
        Server.ClearError();
    }  
    
  3. Specify custom error pages in your web.config file

    <customErrors mode="On" defaultRedirect="myperfectxhtmlerror.aspx" >
    

For more detailed explanations see: How to create custom error reporting pages in ASP.NET

like image 68
Remko Jansen Avatar answered Oct 13 '22 00:10

Remko Jansen


Other than the pretty much spot on answer above you could try:

HTTP Module for Custom Errors

Using this you can specify the output HTML, while this way doesn't require full custom pages it will still require you to write a small amount of HTML.

like image 26
Ryan McDonough Avatar answered Oct 13 '22 00:10

Ryan McDonough