ashx 文件的输出(服务器端)缓存

发布于 2024-10-22 02:33:30 字数 971 浏览 9 评论 0原文

我正在尝试对站点中的所有 ashx 文件启用输出缓存。我试图阻止服务器在每个请求上生成文件 - 而不是试图告诉浏览器缓存文件。

我已将 ashx 文件提炼为以下简单代码:

public class DefaultStyles : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/css";
        StringBuilder styleSheet = new StringBuilder();
        styleSheet.AppendLine(string.Format("/* Generated: {0}, {1} */", DateTime.Now.ToShortDateString(),                                                DateTime.Now.ToLongTimeString()));
        context.Response.Write(styleSheet.ToString());
    }
}

在我的 web.config 文件中,我有以下内容:

<system.webserver>
    <caching enabled="true">
      <profiles>
        <add extension=".ashx" policy="CacheForTimePeriod" duration="00:01:00"  />
      </profiles>
    </caching>
</system.webserver>

尽管如此,我对 ashx 文件发出的每个请求都会生成一个包含当前日期和时间的新版本。

我做错了什么?

谢谢。

I am trying to enable output caching on all ashx files in my site. I'm trying to keep the server from generating the file on each request - NOT trying to tell the browser to cache the file.

I've distilled my ashx file down to this simple code:

public class DefaultStyles : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/css";
        StringBuilder styleSheet = new StringBuilder();
        styleSheet.AppendLine(string.Format("/* Generated: {0}, {1} */", DateTime.Now.ToShortDateString(),                                                DateTime.Now.ToLongTimeString()));
        context.Response.Write(styleSheet.ToString());
    }
}

And in my web.config file, i have this:

<system.webserver>
    <caching enabled="true">
      <profiles>
        <add extension=".ashx" policy="CacheForTimePeriod" duration="00:01:00"  />
      </profiles>
    </caching>
</system.webserver>

Still, every request i make to the ashx file generates a new version with the current date and time in it.

What am i doing wrong?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2024-10-29 02:33:30

也许这会有帮助?

http://objectmix.com/dotnet/393333-output-caching -custom-http-handler.html

这似乎表明,由于您没有使用普通的页面处理程序,因此不会调用输出缓存模块。

西蒙

Maybe this will help?

http://objectmix.com/dotnet/393333-output-caching-custom-http-handler.html

It would seem to suggest that since you're not using the normal page handler, the output caching module isn't invoked.

Simon

流云如水 2024-10-29 02:33:30

实际上我做的一切都是正确的,只是没有在正确的环境中进行测试。当我在 IIS 7 服务器上部署上面的简单示例后,它按照上面的代码按预期工作。应该注意的是,这在我们的 IIS 6 服务器上不起作用。我没有意识到这是一个仅在 IIS 7 中提供的新功能

I was actually doing everything right, just not testing in the right environment. Once i deployed the simple example above on our IIS 7 server, it worked as expected with the code above. It should be noted that this DID NOT work on our IIS 6 server. I had not realized that this was a new feature only available in IIS 7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文