Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.Net缓存破坏方法不起作用_C#_Asp.net_Url_Caching_Iis - Fatal编程技术网

C# ASP.Net缓存破坏方法不起作用

C# ASP.Net缓存破坏方法不起作用,c#,asp.net,url,caching,iis,C#,Asp.net,Url,Caching,Iis,我试图解决浏览器缓存的问题。每当js和css文件中有任何更改时,这些文件都是从浏览器缓存而不是服务器中提供的,我在互联网上进行了研究,从mads krinstinsen那里找到了这些文件 我在我的App\u code文件夹中的一个类中包含了以下类和方法 using System; using System.IO; using System.Web; using System.Web.Caching; using System.Web.Hosting; public class Fing

我试图解决浏览器缓存的问题。每当js和css文件中有任何更改时,这些文件都是从浏览器缓存而不是服务器中提供的,我在互联网上进行了研究,从mads krinstinsen那里找到了这些文件

我在我的
App\u 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; 
  } 
}
后来,我更改了我所有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>
我的IIS网站URL如下所示

http://www.example.com/myClientName/login.aspx
您可能已经注意到IIS上有一个额外级别的url段
(\myClientName\)
,这就是导致问题的原因

我还添加了在URL中添加
myClientName
部分的逻辑,不幸的是,这也不起作用

在IIS托管的网站上,我发现了大量404错误,因为url路径跳过了myClientName部分

更新1

我还尝试了以下同一方法的另一个版本,它检查代码是在iisexpress中运行还是在完整的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;
    }

请告诉我正确的方向。

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

如果使用重写规则,会发生什么?dev上的de-path也会在同一时间上线吗?@Dexion你的意思是像是的。我建议也比较iis/站点配置。此重写规则是首次在配置中引入的。开发配置文件和iis配置文件都具有相同的重写规则,是否应修改它们?我对重写规则惯例了解不多删除dev和live上的规则并检查URL-你需要确定这些规则或其他东西是否增加了额外的文件夹级别。在引用中进行更改是一项繁琐的工作,因为可能有90/100页,每个页面中都有css和js引用。我不允许在其他端口上运行该网站。谢谢
http://www.example.com/myClientName/login.aspx
 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;
    }