So I’m building a web app using C#.NET, and would like to add a version number to file references. For example:
<script src="mysite/scripts/default.123.js"></script>
Each time I modify files in the web app, including cshtml, CSS, JS, or images, is it possible to have that version number incremented dynamically? In other words, how do I get [or create] the version number in the first place? Is it possible?
This is to avoid caching old copies on the client browsers, especially when being served via XHR. For reasons I don’t want to take the time to explain, I am NOT asking for alternative methods, such as dummmy parameters, no-cache meta tags, datetimestamps, CDN’s etc.
I’d like this number to correspond to the most recent version of file that was modified – maybe “build version” isn’t the right word. As Kyle Trauberman, assemblyversion might might work. However will this accomodate for changes to static resources, such as HTML or CSS? What is a good method for that?
To solve this problem, I go with a slightly different approach. Rather than using a “version number”, I simply append the last modified date. To make things convenient, I wrote a helper method:
Then in your views (not sure if you’re using MVC, but the solution is similar in either case), you can use:
Which will output:
Note: Page Speed is not happy with the use of the query string. However, I tried changing it to using a “/” and that resulted in MVC taking over the routing for the file, and preventing IIS from handling it. Not sure how to resolve that.