Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UrlHelper.Content overload that accepts an appendVersion argument #57964

Open
1 task done
tuespetre opened this issue Sep 19, 2024 · 0 comments
Open
1 task done

UrlHelper.Content overload that accepts an appendVersion argument #57964

tuespetre opened this issue Sep 19, 2024 · 0 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@tuespetre
Copy link
Contributor

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

1 participant