浏览器到网站程序

 

上一篇中,介绍ihttpmodule的时候,自定义一个类customhttpmodule继承自ihttpmodule,自定义一个事件,并配合配置文件,就可以执行自定义module中的init方法。我们在浏览一个view视图,并新建一个webform页面,也浏览一下

 

 

 

 我们可以看出来,不管是mvc还是webform,页面解析都是在prerequesthandler和postrequesthandler之间。

配置文件指定映射关系:

  后缀名与处理程序的关系(ihttphandler—-ihttphandlerfactory),http任何一个请求一定是由某一个具体的handler来处理的,不管成功还是失败,以前写aspx,感觉请求访问的是物理地址,其实不然,请求的处理是框架处理的。

所谓管道处理模型,其实就是后台如何处理一个http请求,定义多个事件完成处理步骤,每个事件可以扩展动作(httpmodule),最后有个httphandler完成请求的处理,这个过程就是管道处理模型,还有一个全局的上下文环境,无论参数,中间结果,最终结果,都保存在其中。

直播平台–网页播放–jwplayer–需要一个配置文件.rtmp
在临时文件夹生成一个文件.rtmp 然后配置一下文件mine,当成物理文件访问—临时生成—还得删除

客户端要的是内容—先保存硬盘—返回文件流
如果能直接动态响应 .rtmp
我们可以从请求级出发,避开默认机制

 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(浏览器行为),在白名单里面就正常返回,否则就不正常返回(返回一个授权图片)

 

 

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)
     {
     }
 }

配置文件

<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框架