How to clear ASP.Net error pages?

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

I would like to have automatically generated XHTML code , it would be nice if the web services parsed the answer ... (I'm not interested in writing custom pages).

Is it possible to specify it?

EDIT: EXAMPLE

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

<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 assume the above HTML was created on an ASPX page, is there a place where I can find it? To get inpired and translate it to XHTML, I will use the @Remko 3rd solution to specify a custom error page.

+6
source share
2 answers

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

  • Page Level: Add a Page_Error event handler to the 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 will not get the page with the default error.

  • At the application level: add an Application_Error event handler to 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(); } 
  • Specify custom error pages in web.config

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

For more detailed explanations, see How to Create Custom ASP.NET Error Reporting Pages.

+10
source

Besides the fact that you can answer above, you can try:

HTTP module for custom errors

With this, you can specify HTML output, while this method does not require full user pages, it will still require you to write a small amount of HTML.

+3
source

All Articles