Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Can we have a UrlHelper.Content parameter or overload that appends the file version? #5569

Closed
tuespetre opened this issue Nov 28, 2016 · 4 comments

Comments

@tuespetre
Copy link
Contributor

The TagHelpers for <link> and <script> are awesome. So awesome that I would like to be able to use the same asp-append-version functionality for scripts and styles that have to be added dynamically, such as scripts custom elements that need ES2015 class syntax in browsers that support it but ES5 syntax in browsers that don't. Right now to take advantage of this functionality, we have to declare dummy elements -- either <link>s with bogus rels or <script> with bogus types -- with ids so we can retrieve them in JavaScript and get their src/href values.

Could this functionality be exposed via Url.Content, either via an optional parameter or an overload?

@ShadowDancer
Copy link

@tuespetre as a workaround you may download source, take service that sits behind asp-append-version and wrap it in extension method on UrlHelper.

@Eilon Eilon added this to the Backlog milestone Jan 25, 2017
@Eilon
Copy link
Member

Eilon commented Jan 25, 2017

Definitely a nice feature idea, but not planned for this release.

@axelheer
Copy link

Workaround:

public static string Content(this IUrlHelper url, string contentPath, bool appendVersion)
{
    if (url == null)
        throw new ArgumentNullException(nameof(url));

    var path = url.Content(contentPath);

    if (appendVersion)
    {
        var serviceProvider = url.ActionContext.HttpContext.RequestServices;

        // compare code of LinkTagHelper.cs for details of this kinda hacky stuff...

        var fileProvider = serviceProvider.GetService<IHostingEnvironment>().WebRootFileProvider;
        var cache = serviceProvider.GetService<IMemoryCache>();
        var requestPathBase = url.ActionContext.HttpContext.Request.PathBase;

        var versionProvider = new FileVersionProvider(fileProvider, cache, requestPathBase);

        path = versionProvider.AddFileVersionToPath(path);
    }

    return path;
}

@mkArtakMSFT
Copy link
Member

Hi. Thanks for contacting us.
We're closing this issue as there was no much community interest in this ask for quite a while now.
Also a workaround available.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants