ASP.NET: Programmatically adding <script> tags to include JavaScript files in the master page’s <head> tag


One common task of ASP.NET master pages is the definition of the page’s <head> tag, which could include <script> tags to include external JavaScript files>. If you do not use absolute paths to refer to the JavaScript file, adding a <script> leads to the problem, that the path to the JavaScript file has to consider the directory structure.
Here is a simple code snippet that can be included in the master page’s code behind file to dynamically resolving the paths and adding the corresponding <script> tags:

protected void Page_Load(object sender, EventArgs e)
{
  [...]
  string[] jsFiles = {"~/Path/To/File1.js", "~/Path/To/File2.js"};
  foreach (var jsFile in jsFiles.Reverse()) // reverse the array to add