Skip to main content

MagicConstants by Liesel Thuriot

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

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

Source: https://github.com/LieselThuriot/MagicConstants

Author

note

Liesel Thuriot Alt text

Original Readme

note

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>

About

note

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)

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>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

namespace FolderToCode
{
public static partial class Assets
{

internal const string MyFolderToShow_en_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 Nuget GitHub Repo stars 2024-01-21

2 corecraft Nuget GitHub Repo stars 2024-02-17

3 Datacute.EmbeddedResourcePropertyGenerator Nuget GitHub Repo stars 2024-11-03

4 DotnetYang Nuget GitHub Repo stars 2024-06-29

5 EmbedResourceCSharp Nuget GitHub Repo stars 2023-04-16

6 kli.Localize Nuget GitHub Repo stars 2025-10-01

7 LingoGen Nuget GitHub Repo stars 2024-02-23

8 Maestria.TypeProviders Nuget GitHub Repo stars 2026-04-09

9 MagicConstants Nuget GitHub Repo stars 2026-09-04

10 NFH.FileEmbed Nuget GitHub Repo stars 2025-08-08

11 NotNotAppSettings Nuget GitHub Repo stars 2024-01-26

12 Pinecone.TypedPath Nuget GitHub Repo stars 2026-08-24

13 Podimo.ConstEmbed Nuget GitHub Repo stars 2023-04-16

14 ResXGenerator Nuget GitHub Repo stars 2023-10-02

15 RSCG_JSON2Class Nuget GitHub Repo stars 2024-02-29

16 RSCG_Utils Nuget GitHub Repo stars 2023-04-16

17 Strings.ResourceGenerator Nuget GitHub Repo stars 2025-07-06

18 SvgIconGenerator Nuget GitHub Repo stars 2026-04-04

19 ThisAssembly_Resources Nuget GitHub Repo stars 2023-09-16

20 ThisAssembly.Strings Nuget GitHub Repo stars 2024-07-21

21 TypedPaths Nuget GitHub Repo stars 2026-04-01

22 Weave Nuget GitHub Repo stars 2024-01-27

See category

FilesToCode