浏览器到网站程序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框架