Skip to content

Commit

Permalink
Add FileVersionHashProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Apr 24, 2024
1 parent 42fc6c3 commit 789da12
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DNTCommon.Web.Core/DNTCommon.Web.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>DNTCommon.Web.Core provides common scenarios' solutions for ASP.NET Core applications.</Description>
<VersionPrefix>4.8.0</VersionPrefix>
<VersionPrefix>4.9.0</VersionPrefix>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>net8.0;net7.0;net6.0;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
46 changes: 46 additions & 0 deletions src/DNTCommon.Web.Core/Mvc/FileVersionHashProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.DependencyInjection;

namespace DNTCommon.Web.Core;

/// <summary>
/// This utility class will help you to add your `asp-append-version` like query string value to the .css or .js files.
/// </summary>
public static class FileVersionHashProvider
{
private static readonly string ProcessExecutableModuleVersionId =
Assembly.GetEntryAssembly()!.ManifestModule.ModuleVersionId.ToString("N");

/// <summary>
/// Adds a version hash to a specified file.
/// If you can't use `asp-append-version` directlty in Blazor apps, use this method instead.
/// </summary>
/// <param name="httpContext">Encapsulates all HTTP-specific information about an individual HTTP request</param>
/// <param name="filePath">The path of the .js or .css file to which version should be added</param>
/// <param name="defaultHash">
/// If it's not possible to calculate the hash of the filePath, this value will be used.
/// If you don't specify it, Assembly.GetEntryAssembly()!.ManifestModule.ModuleVersionId will be used for it.
/// </param>
/// <returns></returns>
public static string GetFileVersionedPath(this HttpContext httpContext, string filePath, string? defaultHash = null)
{
ArgumentNullException.ThrowIfNull(httpContext);

var fileVersionedPath = httpContext.RequestServices.GetRequiredService<IFileVersionProvider>()
.AddFileVersionToPath(httpContext.Request.PathBase, filePath);

return IsEmbeddedOrNotFound(fileVersionedPath, filePath)
? QueryHelpers.AddQueryString(filePath, new Dictionary<string, string?>(StringComparer.Ordinal)
{
{
"v", defaultHash ?? ProcessExecutableModuleVersionId
}
})
: fileVersionedPath;
}

private static bool IsEmbeddedOrNotFound(string fileVersionedPath, string filePath)
=> string.Equals(fileVersionedPath, filePath, StringComparison.Ordinal);
}

0 comments on commit 789da12

Please sign in to comment.