代码之家  ›  专栏  ›  技术社区  ›  Ken Mason

缓存问题:动态css文件

  •  4
  • Ken Mason  · 技术社区  · 17 年前

    我有一些css文件需要动态生成(为什么很复杂,但这是必要的)。简单的解决方案是将文件更改为aspx文件,并根据需要使用<%=%>标记来注入我想要的值。

    这很好,直到我们进入缓存。浏览器和服务器似乎拒绝缓存文件。我尝试过操作各种http缓存头,但是浏览器总是在每次页面加载时重新获取文件。服务器总是用HTTP200(和正确的内容)响应,而不是304(文件不变)。

    使用文件示例:

    <html> <head> <link href="/style.aspx" type="text/css" rel="stylesheet"/> ...

    <%@ Page Language="C#" ContentType="text/css" EnableSessionState="False" %>
    <% Response.Cache.SetLastModified(new DateTime(2009, 11, 18, 10, 1, 0)); Response.Cache.SetMaxAge(TimeSpan.FromDays(30)); Response.Cache.SetETag("4ffff353ff67ff1:0"); %>
    …这里是css。。。

    发送到服务器的请求头包括以下内容:

    If-Modified-Since Wed, 18 Nov 2009 18:01:00 GMT
    Cache-Control max-age=0

    Cache-Control private, max-age=2592000
    Last-Modified Wed, 18 Nov 2009 18:01:00 GMT
    Date Wed, 18 Nov 2009 19:36:07 GMT

    有什么想法吗?

    4 回复  |  直到 17 年前
        1
  •  1
  •   IronMannnn    14 年前

        2
  •  0
  •   Bertine    17 年前

    如何缓存.aspx页面,该页面作为css文件使用如下所示:

    <%@ OutputCache Duration="60" Location="Client" VaryByParam="None" %>
    

    How to cache in ASP.NET by using Visual C# .NET

        3
  •  -1
  •   Ray    17 年前

    你试过了吗Response.SetExpires()? 这比设置mod日期和最大年龄简单一点——也许你的浏览器会更喜欢它。

        4
  •  -1
  •   TheQ    16 年前

    编辑:我没意识到这是一个老问题,希望这个问题还能对其他人有所帮助。

    例如:

    <link href="<%=sStyleSheet%>" type="text/css" rel="stylesheet" />
    

    然后在你的代码隐藏中

    Random random = new Random();
    int num = random.Next(100000);
    string sStyleSheet = "/style.aspx?q=" + num.ToString();