c# - How to add a cache buster to all images in an ASP.NET MVC site -


I am trying to improve the performance of a large ASP.NET MVC website and I am currently adding one Looking for the way. The cache-destroying query string is from image requests in such a way that I do not have to go through all the scenes and CSS and change each image reference.

The desired result

I'm using the firebug extension in Firefox to verify whether the cache buster is being added or not I see it is something like this (screenshot taken from another website)

image here Enter the details

Whatever I have tried

Simple answer I was looking for to create a custom HTML module Mr. image prevents the request and then requested URL was the cache adds busting query string. So I have written the following class

  Public category CacheBusterImageHandler: IHttpModule {Public Zero Init (HttpApplication Reference) {context.BeginRequest + = this.BeginRequest; } Public Zero Extraction () {} Public Zero BeginRequest (Object Sender, EventArgs E) {// This conditional test will need to be modified to capture other image types / but for testing purposes it is very good if (HttpTP. Request.Path.EndsWith (".gif", StringComparison.OrdinalIgnoreCase)) {// version number will be generated, but for testing purposes again this is a very good path // WithCacheBuster = string.Format ("{ 0}? V1? 0.0.0 ", HttpContext.Current.Request.Path); HttpContext.Current.RewritePath (pathWithCacheBuster); }}}   

I again registered the module in web.config like this

   ...   

I then verified that the requests were being processed by the module using breakpoint, although when I checked in firebug, image requests did not not < / Em> the cache buster which is linked to the URL. I again decided to read the documentation for the rewrite path and found that it only redirects the request but can not change the requested URL.

Question

Is there a way to add a cache buster to a query string in a HTTP module?

If not, can I get the same results without modifying one reference to an image?

"Is there a way in an HttpModule to add a cache buster to the query string?"

No, it is late in the process when you put it in the page, then you have to change the URL, this is the browser that uses to check whether the image is in the cache or not.

"If not, is there any other way I can get the same result without modifying each context of an image?"

Depending on how you put an image URL in the page, there is no way to change the URL that works in all ways to keep the URL in the page.

You can create a method that calculates the version number / string, and can add all the URLs. This way you only have to make changes once, no image changes every time.

If you want to deploy the cache every time, the method can compile time using the version number or assembly time, or update time of the image file.

Originally:

  & lt; Img src = "/ images / logo.png and lt;% = ImageVersion (" / "image / logo .png")%> Alt = "logo" & gt;   

Use something like this:

  public static string imagerian (string name) {FileInfo info = new FileInfo (HttpContect.Current.MapPath (name)) ; Int Time = (Int) ((info.LastWriteTimeUtc - New DateTime (2000,1,1)) TotalMinutes; Return "? V =" + time.ToString (); }    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -