C# Developeramp;#039;s Guide to ASP.NET, XML, and ADO.NET [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

C# Developeramp;#039;s Guide to ASP.NET, XML, and ADO.NET [Electronic resources] - نسخه متنی

Jeffrey P. McManus; Chris Kinsman

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








Dynamic Handler Assignment


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 an example. Listing 8.17 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 "Chris", the ChrisHandler is returned to ASP.NET to handle the request. If the value of the argument is "Jeffrey", the JeffreyHandler is returned.

Listing 8.17 A Sample HttpHandlerFactory That Returns Different Handlers Based on the Name Parameter


using System;
using System.Web;
using System.Web.UI;
namespace Handlers
{
/// <summary>
/// Summary description for HandlerFactory.
/// </summary>
public class HandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, string requestType,
string url, string pathTranslated)
{
// Check the name property
if(context.Request["Name"] == "Chris")
// If it's Chris return chris
return new ChrisHandler();
else
// Else return Jeff
return new JeffHandler();
}
// required to implement the interface
public void ReleaseHandler(IHttpHandler handler)
{
}
}
/// The ChrisHandler
///
public class ChrisHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<html><body>Chris</body></html>");
}
public bool IsReusable
{
get
{
return true;
}
}
}
/// The JeffHandler
///
public class JeffHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<html><body>Jeff</body></html>");
}
public bool IsReusable
{
get
{
return true;
}
}
}
}

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

/ 106