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

I am trying to reload verified css / js files automatically to prevent cache. I can iterate over the header (which is runat = "server") and add a value for the href link link, but the loop does not capture the 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 tried adding runat = "server" to the script tags, but this way asp.net is trying to compile these files and fail. I can’t add a version every time because there are a lot of files in the project. In addition, code locks are not allowed when runat = "server" is used.

I want to use a common solution, so I no longer need to worry about css / js file versions.

+4
4

script , , . :

<script type="text/javascript" src="path/to/file.js?v=<%= Tools.BuildDate() %>" />

: http://madskristensen.net/post/cache-busting-in-aspnet

+2

<script src="ScriptsFolder/ScriptFile.js?v=<%=Tools.BuildDate() %>"></script>
<link href="StylesFolder/StyleFile.css?v=<%= Tools.BuildDate() %>" rel="stylesheet" />

, Tools.BuildDate(), , script/style, URL- , . url/ScriptsFolder/ScriptFile.js? v = 1234 url/ScriptsFolder/ScriptFile.js? v = 5678. , , , v = URL- . .

+2

MVC Core 1.0 . MVC, .

<link rel="stylesheet" src="wherever.css" asp-append-version="true" />
+2

, ASP.NET( MVC) , . "", . MVC ASP.NET 5, . , .

:

public static class ScriptHelper
{
    public static IHtmlString Render(params string[] paths)
    {
        return RenderFormat(DefaultTagFormat, paths);
    }

    public static IHtmlString RenderFormat(string tagFormat, params string[] paths)
    {
        if (string.IsNullOrEmpty(tagFormat))
        {
            throw new ArgumentException("Tag Format cannot be null");
        }
        if (paths == null)
        {
            throw new ArgumentNullException("Paths cannot be empty");
        }
        if (paths.Any(string.IsNullOrEmpty))
        {
            throw new ArgumentException("paths");
        }
        return BuildHtml(tagFormat, paths);
    }

    private static string defaultFormat = "<script src=\"{0}?ver={1}\"></script>";

    public static string DefaultTagFormat
    {
        get
        {
            return defaultFormat;
        }
        set { defaultFormat = value; }
    }

    private static IHtmlString BuildHtml(string tagFormat, params string[] paths)
    {
        StringBuilder builder = new StringBuilder();
        foreach (string path in paths)
        {
            StringBuilder builder = new StringBuilder();
        foreach (string path in paths)
        {
            // Substitute your logic for version number
            var version = "1234";
            // You could factory this to a concrete type based on file extension etc.
            var fileToOutPut = new VersionedJsFile(path,version,tagFormat);
            builder.Append(fileToOutPut.RenderOutput());
            builder.Append(Environment.NewLine);
        }
        return new HtmlString(builder.ToString());
    }

}

, , .. , ( ) . . "VersionHelper" css/js/etc.

 internal abstract class HtmlFile
{
    public abstract string RenderOutput();
}

internal class VersionedJsFile : HtmlFile
{

    private string _version;
    private string _path;
    private string _format;
    public VersionedJsFile(
        string path, 
        string version,
        string format)
    {
        if (version != null) _version = version;
        if (path != null) _path = path;
        if(!string.IsNullOrEmpty(format))
            _format = format;
    }

    public override string RenderOutput()
    {
        if (!string.IsNullOrEmpty(_path)
            && !string.IsNullOrEmpty(_format))
        {
            string versionedFilePath = string.Format(_format, _path, _version);
            return versionedFilePath;

        }
        return string.Empty;
    }
}

Then add an assistant to the page / main page / page layout:

 <%: ScriptHelper.Render("/scripts/bootstrap.js") %>

As you can see, the helper accepts a params object, so you can immediately apply to multiple files.

Output:

<script src="/scripts/bootstrap.js?ver=1234"></script>

Assistant can be easily enhanced.

+1
source

Source: https://habr.com/ru/post/1613452/


All Articles