You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/mvc/views/view-compilation.md
+81-5Lines changed: 81 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,20 +4,96 @@ author: rick-anderson
4
4
description: Learn how compilation of Razor files occurs in an ASP.NET Core app.
5
5
ms.author: riande
6
6
ms.custom: mvc
7
-
ms.date: 4/8/2020
7
+
ms.date: 04/13/2020
8
8
uid: mvc/views/view-compilation
9
9
---
10
10
# Razor file compilation in ASP.NET Core
11
11
12
12
By [Rick Anderson](https://twitter.com/RickAndMSFT)
13
13
14
-
::: moniker range=">= aspnetcore-3.0"
14
+
::: moniker range=">= aspnetcore-3.1"
15
+
16
+
Razor files with a *.cshtml* extension are compiled at both build and publish time using the [Razor SDK](xref:razor-pages/sdk). Runtime compilation may be optionally enabled by configuring your project.
17
+
18
+
## Razor compilation
19
+
20
+
Build-time and publish-time compilation of Razor files is enabled by default by the Razor SDK. When enabled, runtime compilation complements build-time compilation, allowing Razor files to be updated if they're edited.
21
+
22
+
## Enable runtime compilation at project creation
23
+
24
+
The Razor Pages and MVC project templates include an option to enable runtime compilation when the project is created. This option is supported in ASP.NET Core 3.1 and later.
25
+
26
+
# [Visual Studio](#tab/visual-studio)
27
+
28
+
In the **Create a new ASP.NET Core web application** dialog:
29
+
30
+
1. Select either the **Web Application** or the **Web Application (Model-View-Controller)** project template.
31
+
1. Select the **Enable Razor runtime compilation** check box.
32
+
33
+
# [.NET Core CLI](#tab/netcore-cli)
34
+
35
+
Use the `-rrc` or `--razor-runtime-compilation` template option. For example, the following command creates a new Razor Pages project with runtime compilation enabled:
36
+
37
+
```dotnetcli
38
+
dotnet new webapp --razor-runtime-compilation
39
+
```
40
+
41
+
---
42
+
43
+
## Enable runtime compilation in an existing project
44
+
45
+
To enable runtime compilation for all environments in an existing project:
46
+
47
+
1. Install the [Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation/) NuGet package.
48
+
1. Update the project's `Startup.ConfigureServices` method to include a call to <xref:Microsoft.Extensions.DependencyInjection.RazorRuntimeCompilationMvcBuilderExtensions.AddRazorRuntimeCompilation*>. For example:
## Conditionally enable runtime compilation in an existing project
61
+
62
+
Runtimecompilationcanbeenabledsuchthatit's only available for local development. Conditionally enabling in this manner ensures that the published output:
Nocodechangesareneededintheproject's `Startup` class. At runtime, ASP.NET Core searches for an [assembly-level HostingStartup attribute](xref:fundamentals/configuration/platform-specific-configuration#hostingstartup-attribute) in `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation`. The `HostingStartup` attribute specifies the app startup code to execute. That startup code enables runtime compilation.
*Seethe [runtimecompilationsampleonGitHub](https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation) for a sample that shows making runtime compilation work across projects.
Build- and publish-time compilation of Razor files is enabled by default by the Razor SDK. When enabled, runtime compilation complements build-time compilation, allowing Razor files to be updated if they are edited.
@@ -55,15 +131,15 @@ To enable runtime compilation based on the environment and configuration mode:
55
131
56
132
1. Updatetheproject's `Startup.ConfigureServices` method to include a call to `AddRazorRuntimeCompilation`. Conditionally execute `AddRazorRuntimeCompilation` such that it only runs in Debug mode when the `ASPNETCORE_ENVIRONMENT` variable is set to `Development`:
*Seethe [runtimecompilationsampleonGitHub](https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation) for a sample that shows making runtime compilation work across projects.
142
+
*Seethe [runtimecompilationsampleonGitHub](https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation) for a sample that shows making runtime compilation work across projects.
0 commit comments