Free Jobs
Reference




Dot Net Forums: ASPNET (77) C#.NET (39) VB.NET (8) AJAX (21) Silver Light (1) WCF (10) WPF (1) WWF (1) .NET 3.5 (3) SQL SERVER (38) Other (3)

Topic: Simple sample Hello world example to understand Http Module in ASP.NET

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share





User: dhiraj5079 Post Date: 11 Feb 2012 02:00

Introduction:
Simple sample Hello world example to understand Http Module in ASP.NET



Main Content:
  From a Developers perspective an ASP.NET module is nothing but a class

  that implement System.Web.IHttpModule interface.And is marked in web.config.

  ASP.NET Module enables developers to intercept, participate, modify etc each individual request.

  ASP.NET provides predefined modules and Every request goes through a number of HTTP modules

  that cover various areas of the application like authentication and session intofmation.

  HTTP modules are executed before and after the handler and provide a method for interacting with the request.

  Apart from the Pre-defined module we can also create custom module if we want certain functionality

  to be implemented.

  Custom modules must implement the System.Web.IHttpModule interface

  There may be many situations where you may want to use Custom Module we will discuss one of them.

  Suppose we want to assign the master page of a Page as per request

  then we can assign master page by creating a custom module.

  Add a class to your solution named as SampleModule and Inherit it from IHttpModule as below:

  namespace SampleModuleNameSpace

  public class SampleModuleClass : IHttpModule

  { 

    public void Init(HttpApplication context)

    {

        context.PreRequestHandlerExecute = new EventHandler(context_PreRequestHandlerExecute);

    }

    void context_PreRequestHandlerExecute(object sender, EventArgs e)

    {

        Page page = HttpContext.Current.CurrentHandler as Page;

        if (page != null)

        {

            page.PreInit =new EventHandler(page_PreInit);

        }

    }

    void page_PreInit(object sender, EventArgs e)

    {

        Page page = sender as Page;

        if (page != null)

        {

            page.MasterPageFile = "~/SampleMaster.master";

        }

    }

    public void Dispose()

    {



    }

}



 



For using this module add below in web.config

<system.web>

<httpModules>

   <add name="SampleModuleName" type="SampleModule"/>

</httpModules>

</system.web>





Note that custom module in ASP.NET can also be created as C# library project

If you have created the custom module as  C# library project

then create its dll and put in the bin of you web aplication.

Suppose the dll name is SampleModule.dll and your module class name

is sampleClass then add a entry in web.config as below:

<system.web>

      <httpModules>

         <add name="SampleModule" type="SampleModuleNameSpace.SampleModuleClass, SampleModuledllName" />

      </httpModules>

</system.web>

This kind of custom http module might be useful when you do not want to modify

the existing code but want to deal with each request.Like whenever exceptions

happens send a mail to admin.





 

Conclusion:
The name of the httpModules can be used in Global.ascx file to refer the module.



About Writer:
Hi,I am IT Professional From Mumbai


Writer's Link: http://

Writer's Email:







Comments:
Post Comment





Similar Articles Top ArticlesLatest Articles Other Articles by this Author







www.freejobsreference.com Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)