Purpose or Error Handling ?
Redirecting the remote user to a custom
error page which shows the basic details of the Error and what need to be done
in the next step.This is simply show a understandable message to the User.
Avoid
displaying database related errors : Database related exceptions includes sensitive
details like database server,database name,table,stored procedure etc. So that
an attacker can use.It is advisable to log the exceptions and display a generic
error message.
What is need of handling errors in the Http modules?
1.You can maintain all your login at a single place to handle errors .
2.As
it is a Class library it creates a DLL , all you need to do is add the dll to
your application bin folder , and register it in web.config(i will show to register
it in web.config.)
3.Maintaining would be Quite easy
Example showing
the Error Handling Using Http Module.
First Step is to Create a New Project and Select Class Library and implemt like shown
below...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Globalization;
namespace ErrorHandlingClassLib
{
//the Class is implementing the IHttpModule Interface.
public class ErrorModule:IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
//Raising the Event for handling the Error using application_Error Event
context.Error
+= new EventHandler(application_Error);
}
void application_Error(object sender, EventArgs e)
{
HttpContext
hContext = HttpContext.Current;
//Getting the last occured error for the current
Exception
excp = hContext.Server.GetLastError();
//specfying a path for the Error to log
string
path = "~/ErrorLog/" + DateTime.Today.ToString("dd-MMM-yy") + ".txt";
//Checking if the File Exists or Not.. if file Doesnt Exist then creating the file in that location.
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
}
StreamWriter
objStreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path));
//appending each line of text to the file .
objStreamWriter.WriteLine("\r \n ErrorLog Entry");
objStreamWriter.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
string
Error = "Error in File Path : " + System.Web.HttpContext.Current.Request.Url.ToString() + "\n Error Message : " + excp.Message;
objStreamWriter.WriteLine(Error);
objStreamWriter.WriteLine("************ -------------------- ****************");
//closing the stream........
objStreamWriter.Flush();
objStreamWriter.Close();
}
public void Dispose()
{
throw
new NotImplementedException();
}
#endregion
}
}
After creating the module,
then the final step is to register this in the Web.Config of the Application
for which you are using this ,you need to register under Http modules tag
<httpModules>
<add
name="ErrorModuleClass" type="ErrorModule1.ErrorModuleClass "/>
</httpModules>
Thus
the application errror handling can be managed using Http modules .