Writing Custom errors using HTTPhandlers

Create a class that impliments the IhttpModule  as shown in below 

public class MyErrorClass : 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 = “<br>Offending URL: “ + ctx.Request.Url.ToString () +

“<br>Source: “ + exception.Source + 


“<br>Message: “ + exception.Message + 


“<br>Stack trace: “ + exception.StackTrace;ctx.Response.Write (errorInfo);



Place the below code in web.config file under system.web

 <httpModules >

 <add type=“yourNameSpace.MyErrorClass name=MyErrorClass /> 


change Name Space as per code



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s