An HTTP module is a simple class that can plug themselves into the request-processing pipeline. They do this by hooking into a handful of events thrown by the application as it processes the HTTP request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.
To create an HttpModule, you simply create a class that derives from the System.Web.IHttpModule interface. This interface requires you to implement two methods: Init and Dispose. The simple code snippet below shows an HttpModule that appends a simple message at the end of every page output in your application.
using System;
using System.Web;
namespace WebApplication2
{
public class PageTimerModule : IHttpModule
{
private HttpApplication _application = null;
public void Dispose()
{
}
public void Init(System.Web.HttpApplication context)
{
// save the context
_application = context;
// handle the end request
context.EndRequest += new EventHandler(context_EndRequest);
}
// tag a message at the end of the page
void context_EndRequest(object sender, EventArgs e)
{
string message = string.Format("Generated at {0}",
System.DateTime.Now.ToString());
// stream it at the end of the response
_application.Context.Response.Write(message);
}
}
}
To use this module, we need to modify the web.config and add a reference to this module. This will let ASP.NET know that you want to include the module in the request processing pipeline.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpModules>
<add name="PageTimerModule"
type="WebApplication2.PageTimerModule, WebApplication2"/>
</httpModules>
</system.web>
<!-- For IIS 7, we also need to define it in the web server section-->
<system.webServer>
<modules>
<add name="PageTimerModule"
type="WebApplication2.PageTimerModule, WebApplication2"/>
</modules>
</system.webServer>
</configuration>
That’s it. HttpModules provide you with a simple and effective way to tap into the ASP.NET request pipeline. In the next post we will look into HttpHandlers.
Nice explaination Nikhil. Hope to see more advanced interview questions on ASP.NET.
ReplyDeleteRegards,
Parag
With havin so much written content do you ever run into any problems of plagorism or copyright violation?
ReplyDeleteMy site has a lot of unique content I’ve either authored myself or outsourced but it seems a lot of it is….
popping it up all over the internet without my agreement.
Do you know any solutions to help prevent content from being ripped off? Cara Mengatasi Anak Susah Makan dengan Laperma Platinum
It has been an absolutely fantastic business partner. niche edit backlinks work is phenomenal and the site constantly garners passionate feedback.
ReplyDeleteMake sure you have the required details and/or identification and your bank card if depositing using a Debit/Visa or building society card. You’ll need to provide your email address, a username and password for logging in when signing up to your chosen casino, as well as often a short numeric code in some instances for that added level of extra security. http://www.e-vegas.com
ReplyDeleteNice post with Great Information. I Hope you will be keep posting some articles like this. Look you spend a lot of time or effort in this article. Now it's time to avail shutters in london for more information.
ReplyDeleteAn HTTP module is a class that integrates into the request-processing pipeline, allowing you to respond to specific events during HTTP request handling. By implementing the System.Web.IHttpModule interface, you can create custom behavior, such as appending messages to page outputs. consider finding someone to Pay someone to take my online accounting class for me. If you're busy with coding and need to focus on your studies.
ReplyDelete