Creating Custom HttpModules


An HttpModule is a reusable piece of code that you can plug into the execution chain of an ASP.NET application. You can think of it as a piggyback mechanism whereby any application that receives a specific module can take advantage of the behavior of that module. An added benefit of HttpModules is that they can be reused among multiple applications because you need only to modify web.config to install them.

Understanding the ASP.NET Application Events

When an HttpModule has been installed, its Init method is invoked when the application begins. Within this method, any initialization code is run, and event handlers are attached to various application events. The following is a list of some of the more commonly utilized application events within an HttpModule:

  • AcquireRequestState This event is fired when an application acquires the state associated with a particular request, such as session state.

  • AuthenticateRequest This event is fired after a security module has determined the identity of the current request's user.

  • AuthorizeRequest This event is fired after a security module has determined the authorization for the current user.

  • BeginRequest This is the first event in the HTTP pipeline chain of events that starts when a request comes in for a page.

  • EndRequest This is the last event in the HTTP pipeline chain of events that takes place after a page has been rendered.

There are many more events, and you can find documentation on them all in the MSDN library installed with Visual Studio .NET at the following URL: ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemWebHttpApplicationEventsTopic.htm. The URL might be different if you have a newer version of the MSDN library installed.

Creating a Custom HttpModule

Now that you know what events you can trap using an HttpModule, take a look at some code for a custom HttpModule that can be used on a localized website.

Create a new solution called CustomModule. In that solution, start with a website called CustomModule and a class library called SampleHttpModule. Create a Default.aspx and drop a calendar on the page. Switch to the Page_Load event handler for that page and add the following line of code:

 Response.Write("The price of that item is " +     string.Format("{0:C}", 21.89) ); 

When you run this web page, you get a standard calendar, and you see that the price for an item is $21.89. What if your users could all specify their own local culture, regardless of the culture of the server? To do something like that, create an HttpModule. Add a class to the SampleHttpModule project called SampleModule. The code for that class is shown in Listing 25.2.

Listing 25.2. The SampleModule HttpModule Implementation
 using System; using System.Web; using System.Collections; using System.Globalization; namespace SampleHttpModule {   /// <summary>   /// Summary description for SampleModule.   /// </summary>   public class SampleModule : IHttpModule   {     public void Init( HttpApplication application )     {       application.AcquireRequestState          +=new EventHandler(application_AcquireRequestState);     }     public void Dispose()     {       // take care of anything that needs discarding here     }     private void application_AcquireRequestState(object sender, EventArgs e)     {       CultureInfo ci = new CultureInfo("es-es");       System.Threading.Thread.CurrentThread.CurrentCulture = ci;       System.Threading.Thread.CurrentThread.CurrentUICulture = ci;     }   } } 

During the Init method, we rig up an event handler for the AcquireRequestState event. That handler sets the culture information for the current culture and user interface culture. Here it has been hand-coded to Spanish (Spain), but you could easily do something like read that information from the browser's identification information from a cookie or from some user preference data in your own database. es-es is used here just to make the demonstration easier.

The last step before trying this module is to set it up in web.config with the following tag:

 <httpModules>     <add type="SampleHttpModule.SampleModule, SampleHttpModule"        name="SampleHttpModule" /> </httpModules> 

After building the solution and running the web page, you can probably guess that both the calendar and currency will change to support the new culture. As indicated by Figure 25.3, the currency information for Spain is, in fact, euros. Thanks to the .NET Framework, we don't have to remember details like that.

Figure 25.3. The new web page with its culture set by an HttpModule.




    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net