Saturday, 22 March 2014

What is HTTP Module in asp.net

  • What is HTTP Modules?
       HTTP Modules are simple classes that can play themselves into request - processing pipeline.
  • What are method contains in  HTTP Modules ? 
        Crete HTTP Modules, simply create a class that drives from the System.Web.IHTTPModule Interface.
It require to implement two methods, Init & Dispose.
Init - it is primary method you use to implement functionality. It has only one parameter.
public void Init(HttpApplication context
There are some more another methods as below:
  • Authentication Request
  • Authorization Request
  • Begin Request
  • Error
  • Disposed
  • How to create HTTP Modules ? 
         Providing the example how to create HTTP module, the useful and simple code to modify the output stream.
Create a web Project in Visual Studio and add a class to the App_Code Directory.
namespace Sample
{
    public class AddProcessedMessage : IHttpModule
    {
        private HttpContext _current = null;
        public void Dispose()
        {
            throw new NotImplementedException();
        }
        public void Init(HttpApplication context)
        {
            _current = context.Context;
            context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        }
        void context_PreSendRequestContent(object sender, EventArgs e)
        {
            //Alter the outgoing content by adding a HTML comment
            string message = "";
            _current.Response.Output.Write(message);
        }
    }
}
You need to register it into web config file in configuration section HTTPModule.
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpModules>
      <add name="AddProcessedMessage" type="Sample.AddProcessedMessage, App_Code"/>
    </httpModules>
  </system.web>
</configuration>
When application got executed than after processing of the page comment added with datetime.
HTTP Modules are also used for URL rewriting.

URL Re-writings : It is techniques that allow you to intercept the HTTP request & change the path that was requested to an alternative one.

IIS Wild Card - When IIS receives a request to serve a resource , it check if exist than it pass to appropriate handler. IIS than return the results to the Client. If Resource not exist, IIS return 404 file not found error.

 Click on the link for HTTP Processing Pipeline
  http://getmscodes.blogspot.in/2013/11/how-httprequests-work-in-iis.html
Click on the link for HTTP Handler http://getmscodes.blogspot.in/2013/11/what-is-http-handler-in-aspnet.html

No comments:

Post a Comment

Others

You will like to read it