MagicConstants by Liesel Thuriot
NuGet / site data
Details
Info
Name: MagicConstants
Package Description
Author: Liesel Thuriot
NuGet: https://www.nuget.org/packages/MagicConstants/
You can find more details at https://github.com/LieselThuriot/MagicConstants
Author
Liesel Thuriot

Original Readme
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
.csprojfile 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 servedMagicMimeType- 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>
About
Generates constants from files to return contents. It has extension for routes and more for ASP.NET projects.
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
This is the CSharp Project that references MagicConstants
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
</ItemGroup>
<PropertyGroup>
<MagicConstantsVisibility>internal</MagicConstantsVisibility>
<MagicConstantsRoutes>false</MagicConstantsRoutes>
<MagicConstantsFunctions>false</MagicConstantsFunctions>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MagicConstants" Version="1.0.0-preview-22" />
<AdditionalFiles Include="MyFolderToShow/**/*.json" MagicClass="Assets" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of MagicConstants in Program.cs
using FolderToCode;
Console.WriteLine(Assets.MyFolderToShow_en_a_json);
Console.WriteLine(Assets.MyFolderToShow_ro_a_json);
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Assets.MyFolderToShow_en_a_json.g.cs
- Assets.MyFolderToShow_ro_a_json.g.cs
namespace FolderToCode
{
public static partial class Assets
{
internal const string MyFolderToShow_en_a_json = @"";
}
}
namespace FolderToCode
{
public static partial class Assets
{
internal const string MyFolderToShow_ro_a_json = @"";
}
}
Useful
Download Example (.NET C#)
Share MagicConstants
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MagicConstants
Category "FilesToCode" has the following generators:
1 Chorn.EmbeddedResourceAccessGenerator
2024-01-21
2 corecraft
2024-02-17
3 Datacute.EmbeddedResourcePropertyGenerator
2024-11-03
4 DotnetYang
2024-06-29
5 EmbedResourceCSharp
2023-04-16
6 kli.Localize
2025-10-01
7 LingoGen
2024-02-23
8 Maestria.TypeProviders
2026-04-09
9 MagicConstants
2026-09-04
10 NFH.FileEmbed
2025-08-08
11 NotNotAppSettings
2024-01-26
12 Pinecone.TypedPath
2026-08-24
13 Podimo.ConstEmbed
2023-04-16
14 ResXGenerator
2023-10-02
15 RSCG_JSON2Class
2024-02-29
16 RSCG_Utils
2023-04-16
17 Strings.ResourceGenerator
2025-07-06
18 SvgIconGenerator
2026-04-04
19 ThisAssembly_Resources
2023-09-16
20 ThisAssembly.Strings
2024-07-21
21 TypedPaths
2026-04-01
22 Weave
2024-01-27