zoukankan      html  css  js  c++  java
  • 【续集】塞翁失马,焉知非福:由 Styles.Render 所引发 runAllManagedModulesForAllRequests="true" 的思考

    上一篇中,还有个遗留问题没有解决,就是 ASP.NET MVC MapRoute .htm 不起作用,如果不使用 runAllManagedModulesForAllRequests="true" 的方式,该怎么解决呢?后来找了相关资料,发现了一种解决方案:

      <system.webServer>
        <modules runAllManagedModulesForAllRequests="false" />
        <handlers>
          <add name="HtmlFileHandler" path="*.htm" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
        </handlers>
      </system.webServer>
    

    参考博文:ASP.NET MVC: Route a .html request to an MVC route

    之前探讨过,为什么尽量不要使用 runAllManagedModulesForAllRequests="true",然后我找了两篇相关介绍:

    摘自文中的一段描述:

    This highly recommended fix can cause other problems. These problems come in the form of making all your registered HTTP modules run on every request, not just managed requests (e.g. .aspx). This means modules will run on ever .jpg .gif .css .html .pdf etc.

    runAllManagedModulesForAllRequests 就像 IIS Modules 和请求的一个通道开关,如果这个开关是打开的,那么访问此站点的所有请求都会进入 Modules 中进行处理,这其中就包含一些静态文件的请求,这也是最常见的一种“没必要处理”的请求,因为请求进入 Modules,那就要有相应的程序进行处理,这就造成没必要的性能开销,因为静态文件只是获取展示,完全没必要进行 Modules 处理,小的站点无所谓,当一些很大 PV 站点也这样做的时候,就会对 IIS 的 Modules 程序处理造成一些“压力”,上面有篇博文总结的结论就是 waste(浪费...) 和 potential(潜在...)。

    下面用 Application_BeginRequest 做一个测试,看看对 runAllManagedModulesForAllRequests 配置的不同,会有哪些请求被记录,测试示例代码:

    protected void Application_BeginRequest(object sender,EventArgs e)
    {
        using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
        {
            _testData.WriteLine(Request.Url.ToString());
        }
    }
    

    runAllManagedModulesForAllRequests="fasle",data.txt 记录:

    http://localhost:55127/
    http://localhost:55127/bundles/test2?v=2Fz3B0iizV2NnnamQFrx-NbYJNTFeBJ2GM05SilbtQU1
    http://localhost:55127/bundles/test1?v=MDbdFKJHBa_ctS5x4He1bMV0_RjRq8jpcIAvPpKiN6U1
    

    runAllManagedModulesForAllRequests="true",data.txt 记录:

    http://localhost:55127/
    http://localhost:55127/bundles/test2?v=2Fz3B0iizV2NnnamQFrx-NbYJNTFeBJ2GM05SilbtQU1
    http://localhost:55127/bundles/test1?v=MDbdFKJHBa_ctS5x4He1bMV0_RjRq8jpcIAvPpKiN6U1
    http://localhost:55127/Content/logo_small_1.gif
    http://localhost:55127/Content/logo_small_4.gif
    http://localhost:55127/Content/logo_small_2.gif
    http://localhost:55127/Content/logo_small_3.gif
    

    logo_small_* 图片是我在视图中添加的,这边只是测试静态图片的请求,如果你在一个大型站点下,加上请求测试代码,然后刷新一下页面,你会发现无意义的请求是非常多的。可能你看上面的测试记录,感觉似乎说明不了什么问题,但试想一下,如果一个静态文件很多的站点,然后访问量千万级别的,站点包含的页面也很多,这时候虽然一个很小的问题,但会被无限放大,最后你发现,原来只是一个配置问题。

    就写到这,要改代码了。

  • 相关阅读:
    【Go语言】I/O专题
    【Go语言】集合与文件操作
    【Go语言】LiteIDE使用的个人使用方法
    【Go语言】错误与异常处理机制
    【Go语言】面向对象扩展——接口
    【Go语言】学习资料
    创建型模式(前引)简单工厂模式Simple Factory
    redis demo
    导出CSV格式
    mongo聚合命令
  • 原文地址:https://www.cnblogs.com/xishuai/p/Styles-Render-runAllManagedModulesForAllRequests-true-go-on.html
Copyright © 2011-2022 走看看