Swallow.Blazor.StaticAssetPaths 1.0.0

dotnet add package Swallow.Blazor.StaticAssetPaths --version 1.0.0
                    
NuGet\Install-Package Swallow.Blazor.StaticAssetPaths -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Swallow.Blazor.StaticAssetPaths" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Swallow.Blazor.StaticAssetPaths" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Swallow.Blazor.StaticAssetPaths" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Swallow.Blazor.StaticAssetPaths --version 1.0.0
                    
#r "nuget: Swallow.Blazor.StaticAssetPaths, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Swallow.Blazor.StaticAssetPaths@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Swallow.Blazor.StaticAssetPaths&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Swallow.Blazor.StaticAssetPaths&version=1.0.0
                    
Install as a Cake Tool

Swallow.Blazor.StaticAssetPaths   Source Generator   MIT license


Compile-time correct paths to static web assets

There's a mistake in this Blazor component. Can you spot it?

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>My cool page</title>
        <link rel="stylesheet" href="@Assets["global.css"]" />
        <link rel="stylesheet" href="@Assets["tehme.css"]" />
    </head>

    <body>
        
    </body>
</html>

That's right, it's tehme.css instead of theme.css! Easy to stop, especially when you've got many of these links across many components. But here's the thing: These assets are known at compile time. Why is there no way to have the compiler assist you when writing your code?

Swallow.Blazor.StaticAssetPaths is a source generator to give you just that:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>My cool page</title>
        <link rel="stylesheet" href="@Assets[AssetPaths.Wwwroot.GlobalCss]" />
        <link rel="stylesheet" href="@Assets[AssetPaths.Wwwroot.ThemeCss]" />
    </head>

    <body>
        
    </body>
</html>

Ah, way better. Compile-time constants. What does the generated file look like?

namespace YourProject;

public static class AssetPaths
{
    public static class Wwwroot
    {
        /// <summary>
        /// Points to <c>wwwroot/global.css</c>
        /// </summary>
        public static readonly string GlobalCss = "global.css";

        /// <summary>
        /// Points to <c>wwwroot/theme.css</c>
        /// </summary>
        public static readonly string ThemeCss = "theme.css";
    }
}

You even get a small comment showing you the exact file path!

Usage

Simply reference the package and you're good to go:

<PackageReference Include="Swallow.Blazor.StaticAssetPaths" />

By default, the generator will collect the following files:

  • wwwroot/**/*
  • **/*.razor.js
  • **/*.cshtml.js

If you want to include some files, simply add these items to AdditionalFiles:

<AdditionalFiles Include="Resources/translations.js" SourceItemGroup="Content" />

If you want to exclude items, you can... well... exclude them... or you can override the standard properties DefaultItemExcludes and DefaultExcludesInProjectFolder.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 135 6/30/2025