Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically add version to js files inside asp.net header runat="server"

I'm trying to reload reviewed css/js files automatically to prevent cache. I can loop trough header (which is runat="server") and add a value to link href references but the loop doesn't catch script tags as controls.

if (Page.Header != null)
{
    foreach (var sc in Page.Header.Controls)
    {
        var type = sc.GetType();

        if (type == typeof(HtmlLink))
        {
            ((HtmlLink)sc).Href += "?v=" + Tools.BuildDate();
        }
        else if (type == typeof(HtmlGenericControl))
        {
            ((HtmlGenericControl)sc).Attributes["src"] += "?v=" + Tools.BuildDate();
        }
    }
}

I've tried to add runat="server" to script tags but in this way asp.net tries to compile those files and build fails. I cannot add version every time because the project has many files. Also code blocks is not allowed when runat="server" is applied.

I want to use a commonly accepted solution so I don't have to worry for css/js file versions any more.

like image 241
HasanG Avatar asked Feb 09 '23 16:02

HasanG


1 Answers

In MVC Core 1.0 there is a tag helper for this exact purpose. If you are using a different version of MVC then the previous answers should help.

<link rel="stylesheet" src="wherever.css" asp-append-version="true" />
like image 134
Alex Young Avatar answered Feb 11 '23 15:02

Alex Young