设为首页 加入收藏

TOP

.NET MVC5简介(六)HttpHandler(一)
2019-09-25 11:18:17 】 浏览:83
Tags:.NET MVC5 简介 HttpHandler

浏览器到网站程序

 

上一篇中,介绍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.We
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C#基础之委托 下一篇ASP.NET Core 3.0 原生DI拓展实现..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目