RazorSlices is a lightweight Razor-based template library for ASP.NET Core without MVC, Razor Pages, or Blazor. It's optimized for high-performance, unbuffered rendering with low allocations and is compatible with trimming and native AOT.
src/
RazorSlices/ # Main library (net8.0, ASP.NET Core framework ref)
SourceGenerator/ # Roslyn incremental source generator (netstandard2.0)
samples/
WebApp/ # Main sample app (multi-TFM: net8.0, net9.0)
PagesAndSlices/ # Sample showing Razor Pages + Slices coexistence
RazorClassLibrary/ # Sample Razor Class Library
tests/
RazorSlices/ # Unit tests for library
SourceGenerator/ # Unit tests for source generator
Samples.WebApp/ # Integration tests (WebApplicationFactory)
Samples.WebApp.PublishTests/ # Publish/AOT tests
Benchmarks/ # Benchmarks (BenchmarkDotNet)
BenchmarksWebApps/ # Benchmark projects
src/RazorSlices/IRazorSliceProxy.cs— Interfaces for generated proxy types (IRazorSliceProxyfor no-model,IRazorSliceProxy<TModel>for model slices)src/RazorSlices/IResultExtensions.cs—Results.Extensions.RazorSlice<T>()extension methodssrc/RazorSlices/IUsesLayout.cs— Layout support interfacessrc/RazorSlices/SliceDefinition.cs— Defines a slice, instances emitted at compile time by the source generatorsrc/RazorSlices/SliceDefinitionOfTModel.cs— Slice definition for slices with strongly-typed modelssrc/RazorSlices/RazorSliceProxy.cs— Base class for generated proxy types, implementsIRazorSliceProxyorIRazorSliceProxy<TModel>src/RazorSlices/RazorSliceFactory.cs— Methods for creating factory methods that createRazorSliceinstances fromSliceDefinitionsrc/RazorSlices/RazorSlice.cs— Base class for all slicessrc/RazorSlices/RazorSlice.Partials.cs— Partial rendering supportsrc/SourceGenerator/RazorSliceProxyGenerator.cs— The incremental source generatorsrc/SourceGenerator/RazorDirectiveParser.cs— Parses @inherits/@using from .cshtmlsrc/SourceGenerator/ViewImportsResolver.cs— Hierarchical _ViewImports resolutionsrc/SourceGenerator/ModelTypeResolver.cs— Resolves model types to FQN via Compilationsrc/RazorSlices/build/RazorSlices.props— MSBuild properties for consumerssrc/RazorSlices/build/RazorSlices.targets— MSBuild targets that configure AdditionalTexts for the generatorsrc/Directory.Build.props— Version and package metadata
# Build entire solution
dotnet build
# Run all tests
dotnet test
# Run specific test project
dotnet test tests/SourceGenerator
dotnet test tests/Samples.WebApp
# Run the sample web app
dotnet run --project samples/WebApp- Source generator targets
netstandard2.0— required by Roslyn. NoSpan<T>, no modern .NET APIs. UsesLangVersion: Latestfor language features. - Library targets
net8.0with ASP.NET Core framework reference. - Central package management via
Directory.Packages.props. - Versioning in
src/Directory.Build.props(VersionPrefix). - Generated proxy classes are
public sealedby default. SetRazorSliceProxiesSealed=falseto make thempublic partial. _ViewImports.cshtmlfiles are inherited hierarchically — child directories inherit directives from parents up to the project root.@inheritsfrom the most specific file wins;@usingaccumulates.- All
.cshtmlfiles (except_ViewImportsand_ViewStart) are automatically treated as Razor Slices unlessEnableDefaultRazorSlicesis set tofalse.
The RazorSliceProxyGenerator is an IIncrementalGenerator that:
- Collects all
.cshtmlAdditionalTextfiles - Filters to files with
GenerateRazorSlice=truemetadata - Parses
@inheritsand@usingdirectives from each file and its_ViewImports.cshtmlhierarchy - Resolves model types to fully-qualified names using the
Compilation - Generates proxy classes implementing
IRazorSliceProxy(no model) orIRazorSliceProxy<TModel>(with model)
The generator resolves type names from Razor directives against the compilation:
- Primitives (
bool,int, etc.) → mapped toglobal::System.* - Using aliases (
Models = Namespace.Type) → expanded and resolved - Generic types (
Func<T1, T2>) → parsed recursively, each argument resolved - Array types (
Todo[]) → element type resolved, suffix preserved - Searched in: explicit
@usingnamespaces, then implicit namespaces (System,System.Collections.Generic, etc.)
IRazorSliceProxy— no-model slices, hasstatic abstract RazorSlice CreateSlice()IRazorSliceProxy<TModel>— model slices, hasstatic abstract RazorSlice<TModel> CreateSlice(TModel model)IUsesLayout<TLayout>— layouts without models (constrainsTLayout : IRazorSliceProxy)IUsesLayout<TLayout, TModel>— layouts with models (constrainsTLayout : IRazorSliceProxy<TModel>)