27 oct 2007

Custom Error Pages - IHttpModule

ASP.NET Resources - ASP.NET Custom Error Pages


 using System; using System.Web;  namespace AspNetResources.CustomErrors4 {   public class MyErrorModule : IHttpModule   {     public void Init (HttpApplication app)     {       app.Error += new System.EventHandler (OnError);     }      public void OnError (object obj, EventArgs args)     {       // At this point we have information about the error       HttpContext ctx =  HttpContext.Current;        Exception exception = ctx.Server.GetLastError ();        string errorInfo =              "
Offending URL: " + ctx.Request.Url.ToString () + "
Source: " + exception.Source + "
Message: " + exception.Message + "
Stack trace: " + exception.StackTrace; ctx.Response.Write (errorInfo); // -------------------------------------------------- // To let the page finish running we clear the error // -------------------------------------------------- ctx.Server.ClearError (); } public void Dispose () {} } }
 

To plug our HttpModule into the pipeline we need to add a few lines to web.config:

<?xml version="1.0" encoding="utf-8" ?> <configuration>  <system.web>   <httpModules>    <add type="AspNetResources.CustomErrors4.MyErrorModule,«                                                     CustomErrors4"      name="MyErrorModule" />   </httpModules>  </system.web> </configuration> 

No hay comentarios:

FeedCount

analytics

 
sfrede