Skip to content

UrlHelper.Content overload that accepts an appendVersion argument #57964

Open
@tuespetre

Description

@tuespetre

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I am trying to declare an import map in my web application. So in _Layout.cshtml I will write something like:

<script type="importmap">
    {
        "imports": {
            "my-module": "@Url.Content("~/path/to/my/module.js")"
        }
    }
</script>

However, this does not get the benefits of asp-append-version, and the way to achieve the same result as asp-append-version is not obvious.

Describe the solution you'd like

Currently, the ImageTagHelper, LinkTagHelper, and ScriptTagHelper all privately re-implement this same method:

private string GetVersionedResourceUrl(string url)
{
if (AppendVersion == true)
{
var pathBase = ViewContext.HttpContext.Request.PathBase;
if (ResourceCollectionUtilities.TryResolveFromAssetCollection(ViewContext, url, out var resolvedUrl))
{
url = resolvedUrl;
return url;
}
if (url != null)
{
url = FileVersionProvider.AddFileVersionToPath(pathBase, url);
}
}
return url;
}

private string GetVersionedResourceUrl(string url)
{
if (AppendVersion == true)
{
var pathBase = ViewContext.HttpContext.Request.PathBase;
if (ResourceCollectionUtilities.TryResolveFromAssetCollection(ViewContext, url, out var resolvedUrl))
{
url = resolvedUrl;
return url;
}
if (url != null)
{
url = FileVersionProvider.AddFileVersionToPath(pathBase, url);
}
}
return url;
}

private string GetVersionedSrc(string srcValue)
{
if (AppendVersion == true)
{
var pathBase = ViewContext.HttpContext.Request.PathBase;
if (ResourceCollectionUtilities.TryResolveFromAssetCollection(ViewContext, srcValue, out var resolvedUrl))
{
srcValue = resolvedUrl;
return srcValue;
}
if (srcValue != null)
{
srcValue = FileVersionProvider.AddFileVersionToPath(pathBase, srcValue);
}
}
return srcValue;
}

I think these should be deduplicated into a single implementation. Perhaps UrlHelper itself could contain this bit of logic and expose it via UrlHelper.Content(contentPath, appendVersion), or perhaps it would be better encapsulated by some new service type which could then also be used by UrlHelper.Content(contentPath, appendVersion).

Either way, I would really like to see UrlHelper.Content(contentPath, appendVersion).

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions