ASP.Net缓存清除方法不起作用

Dev*_*osh 5 c# asp.net iis url caching

我正在尝试解决浏览器缓存问题。每当js和css文件中的任何内容发生变化时,这些文件都是通过浏览器缓存而不是服务器提供的,我在Internet上进行了研究,并从mads krinstinsen 找到了这篇很棒的文章。

我在App_Code文件夹的类中包含以下类和方法。

using System; 
using System.IO; 
using System.Web; 
using System.Web.Caching; 
using System.Web.Hosting;

public class Fingerprint 
{ 
  public static string Tag(string rootRelativePath) 
  { 
    if (HttpRuntime.Cache[rootRelativePath] == null) 
    { 
      string absolute = HostingEnvironment.MapPath("~" + rootRelativePath);

      DateTime date = File.GetLastWriteTime(absolute); 
      int index = rootRelativePath.LastIndexOf('/');

      string result = rootRelativePath.Insert(index, "/v-" + date.Ticks); 
      HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute)); 
    }

      return HttpRuntime.Cache[rootRelativePath] as string; 
  } 
}
Run Code Online (Sandbox Code Playgroud)

后来我更改了我所有aspx页面(几乎500个位置)中的引用,如下所示。

<script type="text/javascript" src="<%=Fingerprint.Tag("/Scripts/JQuery/jquery.color.js")%>"></script>
    <script type="text/javascript" src="<%=Fingerprint.Tag("/Scripts/JQuery/jquery.glowbuttons.js?v=1.1")%>"></script>
Run Code Online (Sandbox Code Playgroud)

根据建议,我还添加了以下重写规则,并在IIS中安装了重写模块。

<rewrite>
      <rules>
        <rule name="fingerprint">
          <match url="([\S]+)(/v-[0-9]+/)([\S]+)" />
          <action type="Rewrite" url="{R:1}/{R:3}" />
        </rule>
      </rules>
    </rewrite>
Run Code Online (Sandbox Code Playgroud)

现在我面临的问题

这一切在我的开发环境中都发挥了作用。当我将代码发布到iis(uat和我的本地iis)时,相同的代码不起作用。

该Fingerprint.Tag()方法返回错误的URL。

我的开发网址如下

http://localhost:54992/login.aspx
Run Code Online (Sandbox Code Playgroud)

我的IIS网站URL如下所示

http://www.example.com/myClientName/login.aspx
Run Code Online (Sandbox Code Playgroud)

您可能已经注意到(\myClientName\)IIS上的URL段级别更高,这就是导致问题的原因。

myClientName不幸的是,我还添加了将逻辑部分添加到URL中的逻辑,但该逻辑也不起作用。

在IIS托管的网站上,由于URL路径跳过了myClientName部分,因此出现了很多404错误。

更新1

我还使用以下相同方法的另一个版本进行了尝试,该版本检查代码是否在iisexpress或Full IIS上运行,并相应地生成路径

 public static string Tag(string rootRelativePath)
    {

        if (rootRelativePath.Contains("~"))
            rootRelativePath = rootRelativePath.Replace("~", string.Empty);

        bool isRunningInIisExpress = Process.GetCurrentProcess()
                                .ProcessName.ToLower().Contains("iisexpress");

        if (HttpRuntime.Cache[rootRelativePath] == null)
        {
            string siteAlias = string.Empty;

            if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings["SiteName"]))
                siteAlias = WebConfigurationManager.AppSettings["SiteName"].ToString();
            string absolute = HostingEnvironment.MapPath("~" + rootRelativePath);

            DateTime date = File.GetLastWriteTime(absolute);
            int index = rootRelativePath.LastIndexOf('/');

            string result = rootRelativePath.Insert(index, "/v-" + date.Ticks);
            if (!isRunningInIisExpress)
            {
                siteAlias = "/" + siteAlias;
                result = siteAlias + result;
            }
            if (File.Exists(absolute))
                HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute));
        }

        return HttpRuntime.Cache[rootRelativePath] as string;
    }
Run Code Online (Sandbox Code Playgroud)

请指引我正确的方向。

Dex*_*ion 1

使用相对路径或将站点设置为不同的端口,因此它将以与开发环境中相同的方式在实时环境中运行。