1

I'm using Hudson CI tool to do automated builds and it creates an environment variable ${env.BUILD_NUMBER} for the build number. I want to tack this number into the css links as a query string on many pages of my site. What's the best way of getting this environment variable at build time and replacing it in the code?

The project is an ASP.NET C# website.

Example of outcome:

I commit the code:

<link href="/Content/all.min.css" rel="stylesheet" type="text/css" />

and after build, I'm left with this, if the build number is 15:

<link href="/Content/all.min.css?v=15" rel="stylesheet" type="text/css" />

xdumaine
  • 10,096
  • 6
  • 62
  • 103

2 Answers2

0

You may achieve this using Fody, like I explained in my answer to another question.

Ashraf Sabry
  • 3,081
  • 3
  • 32
  • 29
0

Read the environment variable during Application_Start (Global.asax.cs). I would store it in a static variable. If you are using MVC I would then create an extension method such as:

    public static string VersionedContent(this UrlHelper urlHelper, string contentPath)
    {
        return string.Format("{0}?v={1}", urlHelper.Content(contentPath), YourStaticClassWithVersionInfo.Version);
    }

which you can then use inside your Views to easily append the version query string.

Your view would be like this:

<link href="@Url.VersionedContent("~/Content/Site.css")" rel="stylesheet" type="text/css" />

Let me know if you need more detailed info.

santiagoIT
  • 9,411
  • 6
  • 46
  • 57
  • Soo, I know this is over a year old, but this issue was tabled and has finally come back around. This doesn't work for me, because I don't need to get the environment variable at runtime, I need it at build time. As in, I need MSBuild to replace some placeholder with the enviroment variable during the build, not access the variable when the application is run. – xdumaine Nov 27 '12 at 17:16
  • @roviuser I am much more into node.js these days than asp.net, working on macOs without Visual Studio. Anyhow, look at this question, it should get you going on the right track. http://stackoverflow.com/questions/4450231/can-i-make-a-constant-from-a-compile-time-env-variable-in-csharp – santiagoIT Nov 29 '12 at 02:33