gpt4 book ai didi

asp.net - IIS 和静态内容?

转载 作者:可可西里 更新时间:2023-11-01 15:04:00 27 4
gpt4 key购买 nike

根据 Ultra-Fast ASP.NET: Chapter 3 - Caching :

Files that the browser retrieves from the server should be stored in the browser’s cache as long as possible to help minimize server round-trips.

  • 但是 IIS 如何知道什么是静态内容

    它只是图像、CSS、JS 而不是 ASPX、ashx...吗?

    在什么地方我可以在 IIS 中看到什么被认为是静态,什么不是

  • 页面声明为 <%@ OutputCache 的情况如何?标题(没有 location )?是images , CSSJS其中的源文件被输出缓存为相同的属性?

  • 作为最佳实践,我应该将 future 一年设置为最长到期时间。我应该将其用作网站上所有静态内容的默认设置

所以我这样做了:

Set Common HTTP Response Headers

但稍后,在按下 OK 之后,我找不到任何显示我的摘要菜单:给谁我已经放了一个响应 header (在本例中:css 文件夹)。

目前,为了看到 css文件夹已应用响应 header - 我必须转到 css文件夹再次 --> Http 响应头 -->设置通用标题 -->然后我看到了。它没有写在 web.config 中。

但是如果我为一个文件(例如 Login.aspx)这样做:我确实在 web.config 中看到了它:

<configuration>
<location path="Login.aspx">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseExpires" cacheControlMaxAge="1.00:00:00" httpExpires="Fri, 15 Feb 2013 00:00:00 GMT" />
</staticContent>
</system.webServer>
</location>
</configuration>

最佳答案

我了解你的情况。有时会混淆 IIS 处理文件的方式。 IIS 6 与 IIS 7 也不同,经典应用程序池和集成模式应用程序池也不同。我的经验主要是在 IIS 7.5 上使用集成应用程序池,因此这是我可以最准确评论的环境。

第一个问题

But how does IIS knows what is actually a static content and what is not?

Is it just images , css , js and not ASPX , ashx...?

Where can I see in the IIS what is already considered to be static and what not ?

您可以通过导航到您的网站然后单击“处理程序映射”来检查 IIS 中的文件处理程序列表。默认情况下,这些是从 .Net 基础 web.config 继承的,它位于不同的位置,具体取决于您的 .Net 框架版本。

  • C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
  • C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config

如果被请求的文件尚未显式映射到另一个处理程序,它会落入捕获所有处理程序(*)作为最后一个选项(System.Web.DefaultHttpHandler),它确定它是静态文件还是目录浏览要求。所以静态文件只是尚未绑定(bind)到另一个处理程序的文件。例如,您会看到 *.aspx已经映射到 System.Web.UI.PageHandlerFactory在此默认处理程序之前。因此它将由该处理程序处理,而不被视为静态文件。如果您删除了该映射,如果您确实愿意,技术上您可以将 *.aspx 作为静态文件提供(只是为了证明它是如何工作的)。

但您也可以通过在 web.config 的 httpHandlers 部分中添加一个条目将文件扩展名映射到 System.Web.StaticFileHandler 来明确地将文件类型列为静态文件。在 IIS 中。例如:

<configuration>
<system.webServer>
<handlers>
<add name="StaticHandler" verb="*" path="*.zip" type="System.Web.StaticFileHandler" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>

这个例子使用了 <system.webServer>配置部分,因此它适用于在集成模式中运行的应用程序池。

第二个问题

What about the scenario where a page has been declared with <%@ OutputCache header(without location) . does the images,css,js src files inside of it , are also being output cached with the same properties?

没有。因为该页面作为单独的请求(甚至可能由单独的处理程序)作为服务器提供,所以它可以具有完全不同的缓存 header /提示。从缓存的角度来看,主机页面和它可能使用的资源不相关。

事实上,您甚至可能希望*.html 的缓存时间更短,而*.jpg 或*.png 的缓存时间更长?需要考虑的事情。

第三个问题

As a best prcatice , I should set one year into the future as the maximum expiration time.I should use that as the default for all static content on the site

嗯……我可能不会坚持到一年。一个月怎么样?我会制定这样的全局政策:

<configuration>
<system.webServer>
<staticContent>
<!-- Set expire headers to 30 days for static content-->
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
</staticContent>
</system.webServer>
</configuration>

这与您上面显示的示例相同,但不在 <location> 内元素,相反,它位于根目录中 <configuration>元素,因此它是默认策略。同样,这是针对在集成模式 中运行的应用程序池。有时你还需要开启:

<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<!-- stuff -->
</modules>
</system.webServer>
<system.webServer>

这只是确保通过尊重上述配置元素的托管静态文件处理程序处理静态文件。

编辑以解决评论

您在上面发布的配置对话框的文档位于此处:Configure the HTTP Expires Response Header (IIS 7)

Apparently these settings are saved in C:\Windows\System32\inetsrv\config\applicationHost.config

我没有 IIS7,现在个人在 IIS 7.5 上开发。因此,如果您可以验证此位置是否准确,请发表评论!

关于asp.net - IIS 和静态内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14211992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com