C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Dynamic HTTP Handler Assignment


Posted Date: 04 Nov 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: chaitra Member Level: Bronze    
Rating: 1 out of 5Points: 10



In some cases, you may want to dynamically determine at runtime the appropriate HttpHandler to call for handling a particular request. .NET provides a Factory design pattern that allows you to create a Factory that is responsible for creating the appropriate HttpHandler to deal with the request. This gives you some additional flexibility in creating HttpHandlers. You could look inside an associated file to determine which handler should be called.

The Factory pattern also provides a way for you to potentially pre-create a number of handlers and hand them to ASP.NET when it requests one, without the overhead of creating one each and every time.

Let's look at below sample, it shows a class that implements IHttpHandlerFactory. This class looks for an argument passed as part of the URL. If the value of this argument is "Chaitra", the ChaitraHandler is returned to ASP.NET to handle the request. If the value of the argument is "Jeff", the JeffHandler is returned.




using System;
using System.Web;
using System.Web.UI;

namespace Handlers
{
///
/// Summary description for HandlerFactory.
///

public class HandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, string requestType,
string url, string pathTranslated)
{

// Check the name property
if(context.Request["Name"] == "Chaitra")
// If it's Chaitra return Chaitra
return new ChaitraHandler();
else
// Else return Jeff
return new JeffHandler();
}

// required to implement the interface
public void ReleaseHandler(IHttpHandler handler)
{
}
}

/// The ChaitraHandler
///
public class ChaitraHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Write("Chaitra");
}

public bool IsReusable
{
get
{
return true;
}
}

}

/// The JeffHandler
///
public class JeffHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Write("Jeff");
}

public bool IsReusable
{
get
{
return true;
}
}

}

}



The Chaitra and Jeff handlers just write a simple document with the name Jeff or Chaitra. The HttpHandlerFactory is hooked up in Web.Config the same way an ordinary HttpHandler is hooked up.







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Universal Search Engine for MS Access database
Previous Resource: ASP.NET DataGrid Template Columns with Image Button - Master Details View - Part I
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use