.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating HTTP Module for Exception handling.

Posted By:Sunil Yadav       Posted Date: July 30, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

This article explains how we can create a simple HTTP Module for Exception handling.

HTTPModules are called on every HTTP request made to an application. It works  Once you have placed a HTTPModuel in you application, all the request are passed through the HTTPModule which can be further used to intercept, modify each Http request.ASP.NET uses modules to implement various application features, which includes forms authentication, caching, session state, and client script services. HTTP modules are much similar like Global.asax however HTTP Module has an advantage over Global.asax in terms of reusability. It can be created one time and used in many different applications by adding it in Machine.config or GAC(Global Assembly Cache).

Creating HTTP Module : 

public class ErrorHandlerModule : IHttpModule
        public void Init(HttpApplication application)
            application.Error += new EventHandler(application_Error);

        void application_Error(object sender, EventArgs e)
            HttpContext context = HttpContext.Current;

            Exception exception = context.Server.GetLastError();

            string sPath = "~/errorlog/" + DateTime.Today.ToString("dd-MM-yy") + ".txt";
            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(sPath)))

            StreamWriter objStreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(sPath));
                objStreamWriter.WriteLine("\r\n Error Log Entry : ");
                objStreamWriter.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                string sError = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
                ". Error Message:\n" + exception.Message;

        public void Dispose() { }


 To register the HTTP Module in the application, add the following entry in the web.config.




Reference s:


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend