MagicConstants 1.0.0-preview-22

This is a prerelease version of MagicConstants.
dotnet add package MagicConstants --version 1.0.0-preview-22
                    
NuGet\Install-Package MagicConstants -Version 1.0.0-preview-22
                    
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="MagicConstants" Version="1.0.0-preview-22" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MagicConstants" Version="1.0.0-preview-22" />
                    
Directory.Packages.props
<PackageReference Include="MagicConstants" />
                    
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 MagicConstants --version 1.0.0-preview-22
                    
#r "nuget: MagicConstants, 1.0.0-preview-22"
                    
#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 MagicConstants@1.0.0-preview-22
                    
#: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=MagicConstants&version=1.0.0-preview-22&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=MagicConstants&version=1.0.0-preview-22&prerelease
                    
Install as a Cake Tool

Magic Constants

Magic Constants is a dotnet source generator that generates C# constants from files in your project. It can also minify files and set cache control headers for routes.

This is especially useful for web projects where you want to reference files in your code without hardcoding the paths nor having to manage the string constants.

Global settings in your project:

<PropertyGroup>
  <MagicConstantsVisibility>public</MagicConstantsVisibility>
  <MagicConstantsRoutes>true</MagicConstantsRoutes>
  <MagicConstantsFunctions>false</MagicConstantsFunctions>
  <MagicConstantsRoutesCacheControl>public, max-age=604800</MagicConstantsRoutesCacheControl>
  <MagicConstantsMinify>true</MagicConstantsMinify>
  <MagicConstantsRemovePrefix>wwwroot/</MagicConstantsRemovePrefix>
  <MagicConstantsIncludeContextMetadata>true</MagicConstantsIncludeContextMetadata>
</PropertyGroup>

Specific settings ( Note: if not specified, global settings will be used instead )

<ItemGroup>
  <AdditionalFiles Include="**\*.html" MagicClass="Pages" MagicRemoveRouteExtension="true" MagicCacheControl="public, max-age=86400" MagicMinify="true" MagicRemovePrefix="wwwroot/" />
  <AdditionalFiles Include="**\*.css" MagicClass="Assets" MagicMinify="true" />
  <AdditionalFiles Include="**\*.js" MagicClass="Assets" MagicMinify="true" />
  <AdditionalFiles Include="**\*.svg" MagicClass="Images" />
  <AdditionalFiles Include="**\*.png" MagicClass="Images" />
  <AdditionalFiles Include="**\*.ico" MagicClass="Images" />
</ItemGroup>

When MagicConstantsRoutes is enabled, it will generate an ASP.NET Core route for every file. All you have to do, is call the mapping method:

app.MapViews();

When MagicConstantsFunctions is enabled, it will generate Azure Functions with HTTP triggers for every file. Each function will serve the file content with the appropriate content type and cache control headers.

Note for Azure Functions: If you're using Azure Functions, you may need to disable the built-in source generators to avoid conflicts. Add the following properties to your .csproj file to fall back on reflection:

<PropertyGroup>
  <FunctionsEnableExecutorSourceGen>false</FunctionsEnableExecutorSourceGen>
  <FunctionsEnableWorkerIndexing>false</FunctionsEnableWorkerIndexing>
</PropertyGroup>

Without these settings, the generated functions will not load properly.

Context Metadata

When routes or functions are generated, Magic Constants can optionally add metadata to the HttpContext.Items dictionary:

  • MagicRoute - The route path being served
  • MagicMimeType - The MIME type of the content

This metadata can be useful for logging, telemetry, or custom middleware. You can disable this behavior to reduce overhead:

<PropertyGroup>
  <MagicConstantsIncludeContextMetadata>false</MagicConstantsIncludeContextMetadata>
</PropertyGroup>

Default: true (enabled)

Variable Replacement

Magic Constants also allows you to replace variables inside your .htm, .html, .css and .js files. You can use the following syntax:

<a href="index.html?noCache={MAGIC_TIME}">
<a href="index.html?noCache={MAGIC_HASH}">
{MAGIC_FILE test.html}
  • MAGIC_TIME will use the Unix Timestamp in seconds during build,
  • MAGIC_HASH will try and make a unique but consistent hash out of the timestamp.
  • MAGIC_FILE is a simplistic template inliner.

This will end up as:

<a href="index.html?noCache=1746028631">
<a href="index.html?noCache=LHf1JfsN">
<div>This is the content of test.html</div>
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • 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-preview-22 406 12/9/2025
1.0.0-preview-21 393 12/9/2025
1.0.0-preview-20 375 12/8/2025
1.0.0-preview-19 704 12/8/2025 1.0.0-preview-19 is deprecated because it has critical bugs.
1.0.0-preview-13 144 10/13/2025
1.0.0-preview-12 130 10/13/2025
1.0.0-preview-05 172 4/30/2025
1.0.0-preview-04 141 4/30/2025
1.0.0-preview-03 85 3/15/2025
1.0.0-preview-02 100 12/11/2024
1.0.0-preview-01 111 12/11/2024