Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Cache busting approach not working

I am trying to solve the problem of browser caching. Whenever there are any changes in any of the js and css files, the files are served from the browser cache rather than from server, I researched on internet and found this great post from mads krinstinsen.

I included the following class and method in a class in my App_Code folder.

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; 
  } 
}

Later i changed the references in all my aspx pages(almost 500 locations) like below.

<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>

As suggested i have also added the following rewrite rules and installed rewrite module in IIS.

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

Now the problem i am facing

This all worked a charm in my development environment. When i published the code to the iis (uat and my local iis) the same code did not work.

The Fingerprint.Tag() method returns wrong URLs.

My development URL goes like below

http://localhost:54992/login.aspx

My IIS website URL goes like below

http://www.example.com/myClientName/login.aspx

You might have noticed an extra level of url segment (\myClientName\)on IIS, this is what causing the problem.

I have also added the logic to add the myClientName part in URL unfortunately that also did not work.

On IIS hosted website i gent plenty of 404 errors because the url path skips the myClientName part.

UPDATE 1

I also tried it with the following another version of same method, which checks if the code is running in iisexpress or on Full IIS and generate the paths accordingly

 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;
    }

Please guide me to right direction.

like image 869
Devjosh Avatar asked Nov 08 '22 12:11

Devjosh


1 Answers

Use relative path or set the site a different port, so it will run in live env on the same way as in the dev env.

like image 167
Dexion Avatar answered Nov 14 '22 21:11

Dexion