Saturday, February 26, 2011

ASP.NET interview question :- How do we write a HttpModule ?

ASP.NET interview question :- How do we write a HttpModule ?



Answer:

Again a simple .NET Interview question Writing a HttpModule is a two step process.
Create a class which implements IhttpModule.

  public class clsHttpModule : IHttpModule
 {

 public void Init(HttpApplication context)
 {
    this.httpApp = context;
    httpApp.Context.Response.Clear();
    httpApp.AuthenticateRequest += new EventHandler(OnAuthentication);
    httpApp.AuthorizeRequest += new EventHandler(OnAuthorization);
 ....
 ....
 ....

 }
 void OnAuthorization(object sender, EventArgs a)
 {
     //Implementation
 }
 void OnAuthentication(object sender, EventArgs a)
 {   
     //Implementation
 }
 }

Make a entry in the web.config file.

  <httpModules>
<add name="clsHttpModule" type="clsHttpModule"/>
</httpModules>

Do view my top .NET interview questions and answers

No comments: