瀏覽器到網站程序html
上一篇中,介紹IHttpModule的時候,自定義一個類CustomHttpModule繼承自IHttpModule,自定義一個事件,並配合配置文件,就能夠執行自定義Module中的Init方法。咱們在瀏覽一個View視圖,並新建一個WebForm頁面,也瀏覽一下web
咱們能夠看出來,無論是MVC仍是WebForm,頁面解析都是在PreRequestHandler和PostRequestHandler之間。跨域
配置文件指定映射關係:瀏覽器
後綴名與處理程序的關係(IHttpHandler----IHttpHandlerFactory),Http任何一個請求必定是由某一個具體的Handler來處理的,無論成功仍是失敗,之前寫aspx,感受請求訪問的是物理地址,其實否則,請求的處理是框架處理的。服務器
所謂管道處理模型,其實就是後臺如何處理一個Http請求,定義多個事件完成處理步驟,每一個事件能夠擴展動做(HttpModule),最後有個HttpHandler完成請求的處理,這個過程就是管道處理模型,還有一個全局的上下文環境,不管參數,中間結果,最終結果,都保存在其中。框架
直播平臺--網頁播放--jwplayer--須要一個配置文件.rtmp
在臨時文件夾生成一個文件.rtmp 而後配置一下文件mine,當成物理文件訪問---臨時生成---還得刪除
客戶端要的是內容---先保存硬盤---返回文件流
若是能直接動態響應 .rtmp
咱們能夠從請求級出發,避開默認機制ide
public class CustomRTMPHandler : IHttpHandler { public bool IsReusable => true; public void ProcessRequest(HttpContext context) { context.Response.Write("This is AAAA"); context.Response.ContentType = "text/html"; } }
盜鏈:A網站經過B網站資源展現圖片網站
防盜鏈:B不容許盜鏈請求頁面時會檢測一下urlreferer(瀏覽器行爲),在白名單裏面就正常返回,不然就不正常返回(返回一個受權圖片)url
public class ImageHandler : IHttpHandler { #region IHttpHandler Members public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { // 若是UrlReferrer爲空,則顯示一張默認的禁止盜鏈的圖片 if (context.Request.UrlReferrer == null || context.Request.UrlReferrer.Host == null) { //大部分都是爬蟲 context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/Content/Image/Forbidden.jpg"); } else { // 若是 UrlReferrer中不包含本身站點主機域名,則顯示一張默認的禁止盜鏈的圖片 if (context.Request.UrlReferrer.Host.Contains("localhost")) { // 獲取文件服務器端物理路徑 string FileName = context.Server.MapPath(context.Request.FilePath); context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(FileName); } else { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/Content/Image/Forbidden.jpg"); } } } #endregion }
public class ImageHandlerFactory : IHttpHandlerFactory { public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { string path = context.Request.PhysicalPath; if (Path.GetExtension(path).Equals(".gif")) { return new ImageHandler(); } else if (Path.GetExtension(path) == ".png") { return new ImageHandler(); } else { return new ImageHandler(); } } public void ReleaseHandler(IHttpHandler handler) { } }
配置文件spa
<system.webServer> <!--集成模式使用這個--> <handlers> <!--<add name="config" verb="*" path="*.config" type="System.Web.StaticFileHandler"/>--> <!--帶會兒留個後門--> <add name="rtmp" verb="*" path="*.rtmp" type="MyMVCDemo.Pipeline.CustomRTMPHandler,MyMVCDemo.MVC5"/> <add name="gif" path="*.gif" verb="*" type="MyMVCDemo.Web.Core.PipeLine.ImageHandler,MyMVCDemo.Web.Core" /> <add name="png" path="*.png" verb="*" type="MyMVCDemo.Web.Core.PipeLine.ImageHandler,MyMVCDemo.Web.Core" /> <add name="jpg" path="*.jpg" verb="*" type="MyMVCDemo.Web.Core.PipeLine.ImageHandler,MyMVCDemo.Web.Core" /> <add name="jpeg" path="*.jpeg" verb="*" type="MyMVCDemo.Web.Core.PipeLine.ImageHandler,MyMVCDemo.Web.Core" /> </handlers> <modules>
自定義Handler處理,就是跨域處理各類後綴請求,跨域加入本身的邏輯,若是沒有,請求都到某個頁面,再傳參,而後返回圖片,防盜鏈,僞靜態,RSS,加水印,robot(爬蟲)
MVC裏面不是Controller+Action?實際上是有MVCHandler來處理請求的,期間完成對ACtion的調用
網站啓動時,對RouteCollection進行配置,把正則規則和RouteHandler(提供HttpHandler)綁定,放入RouteCollection,請求來臨時,用RouteCollection進行匹配。在UrlRoutingModule這個勒中
若是路由匹配失敗,仍是繼續原始的Asp.NET 流程,因此WebForm和MVC是共存的,因此也能解釋指定後綴請求須要路由的忽略。
按照添加順序進行匹配,第一個溫和,就直接返回了,後面的就無效了。路由是按照註冊順序進行匹配,遇到第一個溫和的就結束匹配,每一個請求只會被一個路由匹配上
其實所謂的MVC框架,其實就是在ASP.NET管道上擴展的,在PostResolveCache事件擴展了,URLRoutingModule,會在任何請求進來後,先進行路由匹配,若是匹配上了,就指定HttpHandler,沒有路由匹配就仍是走原來的流程。
擴展本身的Route,寫入RouteCollection,能夠自定義規則完成路由,擴展HttpHandler,就能夠隨心所欲,跳出MVC框架