4

How to add version number dynamically to the end of the bundle URLs.

For example:

My bundle url: ~/Bundle/style.min.css?v=123

I wonder, how to dynamically change or add version number before deployment or push or when style.min.css changes.

Is it possible or not?

teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • 2
    Bundling already adds a hash to the URL used in the HTML to refer to the bundle. What problem are you trying to solve? – CodeCaster Apr 13 '15 at 11:11
  • I removed the `Visual Studio` tag from your post, as the bundling-and-minification process in asp.net MVC is a framework feature that is not dependent on the IDE you are using. – Claies Apr 13 '15 at 11:25
  • I am using webessential bundle and i wrote the version end of the bundle url. My problem this, i made the changes on scripts or styles doesnt come if user doesnt use ctrl + f5 because of that i am trying dynamically change version number – Tolga Kısaoğulları Apr 13 '15 at 11:39
  • I suppose [this solution](http://stackoverflow.com/a/27945263/1849444) is what you are looking for. – teo van kot Apr 13 '15 at 13:15

1 Answers1

4

I'm search around and find this article.

Based on it, in your BundleConfig you can redefine Styles.DefaultTagFormat and Scripts.DefaultTagFormat.

So if you need to change version on every build of application you can do this in RegisterBundles method:

var assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
Styles.DefaultTagFormat = "<link href='{0}?v=" + assemblyVersion + "' rel='stylesheet'/>";
Scripts.DefaultTagFormat = "<script src='{0}?v=" + assemblyVersion + "'></script>";

Of course you can get version from your Config.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • I think this method may be works but i am not using web.optimization and bundle.config. I am using bundle of the Web Essentials because of that i am not sure it works for my issue but i will try to try again. Thank you for answer – Tolga Kısaoğulları Apr 14 '15 at 13:06