skip to content

In ASP.NET Core 10, why does AddRazorRuntimeCompilation produce the ASPDEPR003 warning, and how should .cshtml edits reach a running app instead?

level: middleimportance: should knowfreq 38%

answer

  1. Razor SDK compiles .cshtml at build and publish
  2. Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
  3. ASPDEPR003 in .NET 10
  4. runtime compilation disables Hot Reload
  5. dotnet watch for development

basics

~20 s

In .NET 10 Razor runtime compilation is obsolete (ASPDEPR003): views are already compiled at build and publish, runtime compilation disables Hot Reload and isn't meant for production. Use Hot Reload (dotnet watch) in development and rebuild and redeploy for production.

solid answer

~40 s

The Razor SDK compiles every `.cshtml` file into the application at build and publish time. The `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation` package and `AddRazorRuntimeCompilation()` added a second compiler that watched the files and recompiled changed views while the app ran. In ASP.NET Core 10 those APIs carry `[Obsolete]` with diagnostic `ASPDEPR003`: runtime compilation gets no new features, isn't recommended for production, disables .NET Hot Reload and doesn't support global or implicit using directives. The replacement for development is Hot Reload, started with `dotnet watch` or from the IDE, which applies `.cshtml` edits to the running app. For production, a view change is a code change: rebuild, publish and redeploy. When upgrading, delete the package reference and the `AddRazorRuntimeCompilation()` call, which is often wrapped in an `IsDevelopment()` check; with warnings as errors, the build fails until you do.

code

xml · 10 lines
xml
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>
  <ItemGroup>
    <!-- remove: obsolete in .NET 10 (ASPDEPR003) -->
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="10.0.0" />
  </ItemGroup>
</Project>

go deeper

for a junior

Recall that Razor views are compiled when the project builds, and that dotnet watch applies view edits during development.

for a middle

Explain what the RuntimeCompilation package did, why .NET 10 marks it obsolete with ASPDEPR003, and its conflicts with Hot Reload and global usings.

for a senior

Plan the upgrade: remove the package and call, handle TreatWarningsAsErrors, and replace on-server view edits with proper deployments.

for a principal

Move genuinely editable content out of .cshtml files into data or a CMS so view changes always follow the build and release process.

## How Razor views are compiled today A `.cshtml` file is not interpreted at runtime. The **Razor SDK**, part of the web project's build, turns every view and page into C# and compiles it into the application's assembly at **build and publish** time. At runtime the view engine looks views up among those compiled types. This is why a typo in a view breaks the build, and why a published app does not need its `.cshtml` sources to render. ## What runtime compilation was Before Hot Reload existed, editing a view meant rebuilding and restarting. The **`Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation`** package filled that gap: ```csharp var mvc = builder.Services.AddControllersWithViews(); if (builder.Environment.IsDevelopment()) { mvc.AddRazorRuntimeCompilation(); // ASPDEPR003 in .NET 10 } ``` It added file watchers over the `.cshtml` files and a runtime Razor compiler; when a file changed, the view was recompiled and the new version served. The docs also showed how to enable it for all environments, which let views be patched on a running server. ## What changed in ASP.NET Core 10 The runtime-compilation APIs are marked `[Obsolete]` with diagnostic id **`ASPDEPR003`**. Using them produces: - warning ASPDEPR003: Razor runtime compilation is obsolete and is not recommended for production scenarios. For production scenarios, use the default build time compilation. For development scenarios, use Hot Reload instead. The affected APIs include both `AddRazorRuntimeCompilation` overloads (on `IMvcBuilder` and `IMvcCoreBuilder`), `MvcRazorRuntimeCompilationOptions`, `FileProviderRazorProjectItem` and `AssemblyPartExtensions`. It is a source-compatibility break: existing apps still run, but a build with `TreatWarningsAsErrors` fails until the call is removed. The reasons given are practical: 1. **Hot Reload replaced it** for development years ago and is the supported path. 2. Runtime compilation **disables Hot Reload** when it is enabled, so the two fight each other. 3. It **does not support global `using` directives or implicit usings**, which modern templates rely on, so views that compile at build time can fail at runtime compilation. 4. It receives **no new Razor features** and is **not recommended for production**. ## Migrating 1. Remove the `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation` `PackageReference`. 2. Remove `AddRazorRuntimeCompilation()` and any `MvcRazorRuntimeCompilationOptions` configuration (often extra `FileProviders` pointing at another project's views). 3. Develop with **Hot Reload**: run `dotnet watch` (or start the app from Visual Studio with Hot Reload on); `.cshtml` edits are applied to the running app, and an unsupported edit prompts a restart. 4. Treat a production view change like any code change: rebuild, publish, deploy. If the business needs content editable without a deployment, that content belongs in a database or CMS rendered by the views, not in editable `.cshtml` files on the server. If a migration has to ship before the cleanup, `<NoWarn>$(NoWarn);ASPDEPR003</NoWarn>` in the project file silences the warning temporarily; record a follow-up to remove the API, because obsolete APIs are candidates for removal in a later major version. ## Hot Reload in practice Hot Reload works on the running process: `dotnet watch` builds the app, starts it, and on each save applies the change. Razor edits to views, layouts and partials are applied in place. An edit the runtime cannot apply, a *rude edit* such as some signature changes in C#, makes `dotnet watch` offer a restart. Visual Studio and other IDEs offer the same through their Hot Reload button. Because runtime compilation disables Hot Reload when it is enabled, leaving `AddRazorRuntimeCompilation()` in a Development branch actively makes the inner loop worse after an upgrade, not just noisier. ## Checking an upgraded solution Search the solution for three things: a `PackageReference` to `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation`, calls to `AddRazorRuntimeCompilation`, and the `ASPNETCORE_HOSTINGSTARTUPASSEMBLIES` setting in `launchSettings.json` that some projects used to switch runtime compilation on without code. Remove all three, rebuild, and confirm the warnings are gone. ## Interview angle - Say that views are **compiled at build time** by default, and runtime compilation was an opt-in extra. - Name **ASPDEPR003** and the .NET 10 obsoletion, and **Hot Reload via `dotnet watch`** as the replacement. - Explain why editing views on a production server was always a risk: no build validation, file watchers in production, and drift between what is deployed and what is in source control.

  • Why might a view that builds fine fail under runtime compilation?
    Runtime compilation does not support global `using` directives or implicit usings. A view relying on a namespace imported that way compiles at build time, where the Razor SDK sees the project's global usings, but fails when the runtime compiler rebuilds it.
  • Does removing runtime compilation mean .cshtml edits need a full restart while developing?
    No. Hot Reload, started with `dotnet watch` or from the IDE, applies Razor edits to the running app, and asks to restart only for edits it cannot apply. Runtime compilation actually disables Hot Reload when it is enabled.

  • Without runtime compilation, .cshtml files are interpreted on every request.
  • AddRazorRuntimeCompilation is the recommended way to see view edits in .NET 10.
  • Runtime compilation and Hot Reload work together for faster development.
  • Editing .cshtml files on the production server is a supported deployment path.
  • ASPDEPR003 means runtime compilation was removed and the app no longer starts.

Assumes ASP.NET Core 10: AddRazorRuntimeCompilation and the related runtime-compilation APIs became obsolete with diagnostic ASPDEPR003; in .NET 8 and 9 they compiled without a warning.