AspNetCore.VersionedAssets allows you to easily version your static assets


Keywords
cdn, asp.net, core, dotnet
License
Apache-2.0
Install
Install-Package AspNetCore.VersionedAssets -Version 1.0.0-rc2-004

Documentation

ASP.NET Core - VersionedAssets

VersionedAssets is middleware and taghelper for easier integration with CDN configured for origin pull. TagHelper creates asset urls with content version hash burned in url (for cache busting). Middleware handles requests comming from CDN and sets caching headers acordingly.

Following example assumes your CDN is configured with origin pull to base url http://[your.site]/static.

TagHelper generated url: http://[your.cdn]/[version-hash]/bundles/app.js Origin pull url: http://[your.side]/static/[version-hash]/bundles/app.js

Startup.cs configuration

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging();
        services.AddMvc();     
        services.AddVersionedAssets();       
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(LogLevel.Debug);

        // respond to cdn origin pull asset requests in the form /static/[hash]/* 
        app.UseVersionedAssets()
            .WithUrlPrefix("//[your.cdn]")

        app.UseMvcWithDefaultRoute();
    }
}

View configuration

@addTagHelper "*, AspNetCore.VersionedAssets"
<!DOCTYPE html>
<html lang="cs">
<head>
  <meta charset="UTF-8">
  <title>SPA</title>
</head>
<body>
  <script src="~/bundles/app.js" asset-version="FileVersion"></script>
</body>
</html>