對於http請求原理的朋友應該瞭解IIS上後綴映射/*當我們請求一個*.aspx的文件時,此時會映射到System.Web.UI.PageHandlerFactory類上進行處理,而對*.ashx的請求將映射到System.Web.UI.SimpleHandlerFactory類中(這兩個類都是繼承...
對於http請求原理的朋友應該瞭解IIS上後綴映射
/*當我們請求一個*.aspx的文件時,此時會映射到System.Web.UI.PageHandlerFactory類上進行處理,而對*.ashx的請求將映射到System.Web.UI.SimpleHandlerFactory類中
(這兩個類都是繼承於IHttpHandlerFactory介面的)*/
下麵用一線圖來直觀的看一下
圖引用:http://www.cnblogs.com/zhili/p/FactoryMethod.html
為了更直觀的代碼DEOM我把.net部分源碼弄出來了
/*當我們請求一個*.aspx的文件時,此時會映射到System.Web.UI.PageHandlerFactory類上進行處理,而對*.ashx的請求將映射到System.Web.UI.SimpleHandlerFactory類中 (這兩個類都是繼承於IHttpHandlerFactory介面的)*/ //抽象工廠 public interface IHttpHandlerFactory { //返回介面---->抽象產品角色 IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated); void ReleaseHandler(IHttpHandler handler); } //具體工廠 internal class SimpleHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory { public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path) { return ((IHttpHandlerFactory2)this).GetHandler(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path); } //其他方法省略掉了..... } //具體工廠 public class PageHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory { public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path) { return this.GetHandlerHelper(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path); } //其他方法省略掉了..... } //抽象產品角色 public interface IHttpHandler { bool IsReusable{get;} void ProcessRequest(HttpContext context); } //頁面aspx(具體頁面產品) public partial class SystemManager_WebsitePublishing_publishServerFileManageList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } public class Page : IHttpHandler { } /*-------------------------------------------------------*/ //一般處理程式ashx(具體一般處理程式產品) public class getPublishServerQueueInfo : IHttpHandler { public void ProcessRequest (HttpContext context) { } public bool IsReusable { get { return false; } } }
//沒有在場景用的小例子:
//抽象產品 public interface ILight { void TurnOn(); void TurnOff(); } //具體的產品類:燈泡 public class BulbLight:ILight { public void TurnOn() { Console.WriteLine("BulbLight turns on."); } public void TurnOff() { Console.WriteLine("BulbLight turns off."); } } // 具體的產品類:燈管 public class TubeLight:ILight { public void TurnOn() { Console.WriteLine("TubeLight turns on."); } public void TurnOff() { Console.WriteLine("TubeLight turns off."); } } //抽象的工廠類 public interface ICreator { ILight CreateLight(); } //具體燈泡工廠 public class BulbCreator:ICreator { public ILight CreateLight() { return new BulbLight(); } } //具體燈管工廠 public class TubeCreator:ICreator { public ILight CreateLight() { return new TubeLight(); } } static void Main(string[] args) { //先給我來個燈泡 ICreator creator = new BulbCreator(); ILight light = creator.CreateLight(); light.TurnOn(); light.TurnOff(); //再來個燈管看看 creator = new TubeCreator(); light = creator.CreateLight(); light.TurnOn(); light.TurnOff(); }
把上面兩個看一看 比較一下 是不是這麼一回事啊!(註:下麵這個例子引用博客園某位博友的代碼)