RazorSlices: Render Razor Views in ASP.NET Core Minimal APIs
Razor Slices is a lightweight, AOT-friendly Razor template engine for ASP.NET Core Minimal APIs. Set it up, use layouts and partials, and when to use it.
Minimal APIs offer a feature set that rivals ASP.NET Core MVC and Razor Pages, but they don’t include a built-in way to render Razor templates as HTML. That’s the gap Razor Slices fills. The project is written by Damian Edwards, an ASP.NET Core team member with a long history of working on the Razor view engine, and it brings Razor rendering to Minimal APIs the way MVC, Razor Pages, and Blazor users enjoy it today.
What is Razor Slices?
Razor Slices (the RazorSlices NuGet package) is a lightweight, high-performance Razor template engine for ASP.NET Core. It renders HTML directly from Minimal APIs, middleware, and other endpoints, with no MVC, Razor Pages, or Blazor. Each .cshtml file compiles to a strongly-typed slice you render with a generated Create() method, and it supports trimming and native AOT.
Updated November 2026 for Razor Slices 0.12.2, the current stable release (targets .NET 8 and .NET 10). Features that exist only on the project’s main branch are labeled as such.
In this post, we’ll see how to set up Razor Slices in your ASP.NET Core applications and think about the pros and cons of its usage. If you want a refresher on the view engine itself first, see What Is Razor.
Getting Started with Razor Slices
In a Minimal API project targeting .NET 8 or later, start by installing the Razor Slices package. You can do this with the dotnet add package RazorSlices command or by adding the appropriate PackageReference in your .csproj file.
<PackageReference Include="RazorSlices" Version="0.12.2" />Next, create a folder at the root of your ASP.NET Core application. It can be named anything you like. I chose Slices as it was what the documentation recommended. This directory is where you’ll place the Razor views you want to render from your Minimal API endpoints.
Within the Slices folder, create a new file named _ViewImports.cshtml. The view imports file lets you create a shared context for all views within the folder, including namespaces, base classes, and tag helpers. Here’s the contents of my file.
@inherits RazorSlice
@using System.Globalization;@using Microsoft.AspNetCore.Razor;@using Microsoft.AspNetCore.Http.HttpResults;@using RazorSlices;
@tagHelperPrefix __disable_tagHelpers__:@removeTagHelper *, Microsoft.AspNetCore.Mvc.RazorYou’ll notice two strange things when looking at this file:
- The
@inheritsvalue isRazorSlice, the base class every slice derives from. - Tag helpers are removed and obfuscated. That’s because they’re not supported in this model. If you’re porting a Razor Pages implementation over to Razor Slices, expect to make some modifications or concessions.
Next, you’ll want to create a new Hello.cshtml view in the Slices folder.
@inherits RazorSlice<DateTime><!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <title>Hello from Razor Slices!</title></head><body><p> Hello from Razor Slices! The time is @Model</p></body></html>Note that the view also uses the @inherits directive, but this time with a generic type argument. The generic type is the type of the @Model instance.
Razor Slices ships a source generator that inspects each .cshtml file and emits a strongly-typed proxy for it. The proxy exposes a Create method whose signature matches your model:
- Slices with a model get
Create(ModelType model), e.g.MyApp.Slices.Hello.Create(DateTime.Now) - Slices without a model get a parameterless
Create()
That gives you compile-time validation that you’re passing the right model type, with no magic strings. Because a slice implements IResult, you can return it directly from a Minimal API route handler.
var builder = WebApplication.CreateBuilder(args);var app = builder.Build();
app.MapGet("/", () => MyApp.Slices.Hello.Create(DateTime.Now));
app.Run();And that’s it! You are now rendering Razor views through your Minimal API endpoint, with full IntelliSense and compile-time checking on the model.
The Pros of Razor Slices
The most obvious benefit is that you can use Razor to generate HTML results in your Minimal API endpoints. I might be biased, but I love the Razor syntax and the benefits it brings to the web development experience.
Your Razor views can also use the @inject directive to pull dependencies into the rendering process.
Performance is a headline feature. Razor Slices renders unbuffered with low allocations, writes primitives like numbers without boxing or calling ToString() on everything, and can render straight to a PipeWriter, Stream, TextWriter, StringBuilder, or string. It also has full support for trimming and native AOT with Minimal APIs.
Partials, Layouts, and Sections in Razor Slices
Razor Slices isn’t limited to single, standalone templates. You can compose views much like you would in MVC.
Partials. You can render a slice from within another slice, which is how you compose and componentize views.
@(await RenderPartialAsync<MyPartial>())Layouts and sections. Layouts are supported through RazorLayoutSlice (or RazorLayoutSlice<TModel>). The layout renders the child with @await RenderBodyAsync() and can expose named sections.
@inherits RazorLayoutSlice<LayoutModel><!DOCTYPE html><html lang="en"><head> <title>@Model.Title</title> @await RenderSectionAsync("head")</head><body> @await RenderBodyAsync()</body></html>A slice opts into a layout by implementing IUsesLayout<TLayout> (or IUsesLayout<TLayout, TModel>). The MVC-style @section directive isn’t supported because it conflicts with how slices render, so sections run through RenderSectionAsync and ExecuteSectionAsync instead.
Coming next: features on the main branch
These aren’t in the 0.12.2 NuGet package yet, but they’re documented on the project’s main branch and should arrive in a future release:
- Minimal slices. Inherit
RazorSliceBaseorRazorSliceBase<TModel>for an even leaner path when you don’t need@inject, layouts, or partials. - Shareable templated helpers. Define reusable markup on a public interface, implement it in a
.cshtmlfile, and call it as@Helpers.UiHelpers.StatusBadge(...). Helpers can be shared across projects through a Razor class library. - Experimental streaming Tag Helpers. A Razor Slices–specific Tag Helper contract, separate from MVC Tag Helpers.
If you want to try them now, the README explains how to use the project’s CI builds.
The Cons of Razor Slices
Razor Slices was never meant to be a one-to-one replacement for MVC or Razor Pages, so a few features are off the table by design. It skips MVC Tag Helpers and View Components (both tied to MVC and heavy), the @model directive (the Razor compiler can’t use it alongside a custom @inherits base type), and the @section and @attribute [Authorize] directives.
A new paradigm means leaving behind the old one. If you depend on third-party Razor libraries built for MVC or Razor Pages, they likely won’t work here.
Conclusion
Razor Slices is worth checking out if you want to render HTML a big step above string interpolation in your API endpoints. You get the Razor syntax you love, expressions, control structures, looping, and code blocks, plus partials, layouts, @inject, and native AOT, all without dragging in MVC. For most HTML-rendering needs in a Minimal API, that’s more than enough. Go in knowing it’s intentionally not a drop-in MVC replacement, and that’s OK.
If you need interactive components instead of plain HTML, look at returning Blazor components with RazorComponentResult. And if you’re templating text outside of HTTP responses entirely, generating output with the Razor engine is another option.
Frequently asked questions
Is Razor Slices production-ready?
It’s still versioned 0.x, but 0.12.2 is a stable, non-preview release that targets .NET 8 and .NET 10, and the repository is actively maintained. Pin the version and read the release notes before upgrading, since 0.x releases can include breaking changes.
Does Razor Slices support Tag Helpers?
Not MVC Tag Helpers. The recommended _ViewImports.cshtml disables them. The main branch has an experimental streaming Tag Helper model built specifically for Razor Slices, but it isn’t in the current NuGet release.
What’s the difference between Razor Slices and RazorComponentResult?
Razor Slices renders .cshtml templates with no MVC or Blazor dependency, and it’s built for low-allocation, AOT-friendly HTML output. RazorComponentResult renders Blazor components (.razor) from a Minimal API endpoint, which is a better fit if you already have a component library.
Can I use @model in a Razor Slice?
No. Declare the model type with @inherits RazorSlice<TModel> instead, and the generated Create(model) method gives you compile-time checking.
Check out the library at Razor Slices and let me know your thoughts. As always, thanks for reading and sharing my blog posts.
Around the web
Comments
Loading comments from GitHub Discussions…