|
2 | 2 | title: Razor file compilation in ASP.NET Core |
3 | 3 | author: rick-anderson |
4 | 4 | description: Learn how compilation of Razor files occurs in an ASP.NET Core app. |
5 | | -monikerRange: '>= aspnetcore-1.1' |
6 | 5 | ms.author: riande |
7 | 6 | ms.custom: mvc |
8 | | -ms.date: 12/05/2019 |
| 7 | +ms.date: 4/8/2020 |
9 | 8 | uid: mvc/views/view-compilation |
10 | 9 | --- |
11 | 10 | # Razor file compilation in ASP.NET Core |
12 | 11 |
|
13 | 12 | By [Rick Anderson](https://twitter.com/RickAndMSFT) |
14 | 13 |
|
15 | | -::: moniker range="= aspnetcore-1.1" |
16 | | - |
17 | | -A Razor file is compiled at runtime, when the associated MVC view is invoked. Build-time Razor file publishing is unsupported. Razor files can optionally be compiled at publish time and deployed with the app—using the precompilation tool. |
18 | | - |
19 | | -::: moniker-end |
20 | | - |
21 | | -::: moniker range="= aspnetcore-2.0" |
22 | | - |
23 | | -A Razor file is compiled at runtime, when the associated Razor Page or MVC view is invoked. Build-time Razor file publishing is unsupported. Razor files can optionally be compiled at publish time and deployed with the app—using the precompilation tool. |
24 | | - |
25 | | -::: moniker-end |
26 | | - |
27 | | -::: moniker range=">= aspnetcore-2.1 <= aspnetcore-2.2" |
28 | | - |
29 | | -A Razor file is compiled at runtime, when the associated Razor Page or MVC view is invoked. Razor files are compiled at both build and publish time using the [Razor SDK](xref:razor-pages/sdk). |
30 | | - |
31 | | -::: moniker-end |
32 | | - |
33 | 14 | ::: moniker range=">= aspnetcore-3.0" |
34 | 15 |
|
35 | 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 application. |
36 | 17 |
|
37 | | -::: moniker-end |
38 | | - |
39 | 18 | ## Razor compilation |
40 | 19 |
|
41 | | -::: moniker range=">= aspnetcore-3.0" |
42 | | - |
43 | 20 | 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. |
44 | 21 |
|
45 | | -::: moniker-end |
46 | | - |
47 | | -::: moniker range=">= aspnetcore-2.1 <= aspnetcore-2.2" |
48 | | - |
49 | | -Build- and publish-time compilation of Razor files is enabled by default by the Razor SDK. Editing Razor files after they're updated is supported at build time. By default, only the compiled *Views.dll* and no *.cshtml* files or references assemblies required to compile Razor files are deployed with your app. |
50 | | - |
51 | | -> [!IMPORTANT] |
52 | | -> The precompilation tool has been deprecated, and will be removed in ASP.NET Core 3.0. We recommend migrating to [Razor Sdk](xref:razor-pages/sdk). |
53 | | -> |
54 | | -> The Razor SDK is effective only when no precompilation-specific properties are set in the project file. For instance, setting the *.csproj* file's `MvcRazorCompileOnPublish` property to `true` disables the Razor SDK. |
55 | | -
|
56 | | -::: moniker-end |
57 | | - |
58 | | -::: moniker range="= aspnetcore-2.0" |
59 | | - |
60 | | -If your project targets .NET Framework, install the [Microsoft.AspNetCore.Mvc.Razor.ViewCompilation](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation/) NuGet package: |
61 | | - |
62 | | -[!code-xml[](view-compilation/sample/DotNetFrameworkProject.csproj?name=snippet_ViewCompilationPackage)] |
63 | | - |
64 | | -If your project targets .NET Core, no changes are necessary. |
65 | | - |
66 | | -The ASP.NET Core 2.x project templates implicitly set the `MvcRazorCompileOnPublish` property to `true` by default. Consequently, this element can be safely removed from the *.csproj* file. |
67 | | - |
68 | | -> [!IMPORTANT] |
69 | | -> The precompilation tool has been deprecated, and will be removed in ASP.NET Core 3.0. We recommend migrating to [Razor Sdk](xref:razor-pages/sdk). |
70 | | -> |
71 | | -> Razor file precompilation is unavailable when performing a [self-contained deployment (SCD)](/dotnet/core/deploying/#self-contained-deployments-scd) in ASP.NET Core 2.0. |
72 | | -
|
73 | | -::: moniker-end |
74 | | - |
75 | | -::: moniker range="= aspnetcore-1.1" |
76 | | - |
77 | | -Set the `MvcRazorCompileOnPublish` property to `true`, and install the [Microsoft.AspNetCore.Mvc.Razor.ViewCompilation](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation/) NuGet package. The following *.csproj* sample highlights these settings: |
78 | | - |
79 | | -[!code-xml[](view-compilation/sample/MvcRazorCompileOnPublish.csproj?highlight=4,10)] |
80 | | - |
81 | | -::: moniker-end |
82 | | - |
83 | | -::: moniker range="<= aspnetcore-2.0" |
84 | | - |
85 | | -Prepare the app for a [framework-dependent deployment](/dotnet/core/deploying/#framework-dependent-deployments-fdd) with the [.NET Core CLI publish command](/dotnet/core/tools/dotnet-publish). For example, execute the following command at the project root: |
86 | | - |
87 | | -```dotnetcli |
88 | | -dotnet publish -c Release |
89 | | -``` |
90 | | - |
91 | | -A *\<project_name>.PrecompiledViews.dll* file, containing the compiled Razor files, is produced when precompilation succeeds. For example, the screenshot below depicts the contents of *Index.cshtml* within *WebApplication1.PrecompiledViews.dll*: |
92 | | - |
93 | | - |
94 | | - |
95 | | -::: moniker-end |
96 | | - |
97 | 22 | ## Runtime compilation |
98 | 23 |
|
99 | | -::: moniker range="= aspnetcore-2.1" |
100 | | - |
101 | | -Build-time compilation is supplemented by runtime compilation of Razor files. ASP.NET Core MVC will recompile Razor files when the contents of a *.cshtml* file change. |
102 | | - |
103 | | -::: moniker-end |
104 | | - |
105 | | -::: moniker range="= aspnetcore-2.2" |
106 | | - |
107 | | -Build-time compilation is supplemented by runtime compilation of Razor files. The <xref:Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions> <xref:Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions.AllowRecompilingViewsOnFileChange> gets or sets a value that determines if Razor files (Razor views and Razor Pages) are recompiled and updated if files change on disk. |
108 | | - |
109 | | -The default value is `true` for: |
110 | | - |
111 | | -* If the app's compatibility version is set to <xref:Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1> or earlier |
112 | | -* If the app's compatibility version is set to <xref:Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2> or later and the app is in the Development environment <xref:Microsoft.AspNetCore.Hosting.HostingEnvironmentExtensions.IsDevelopment*>. In other words, Razor files wouldn't recompile in non-Development environment unless <xref:Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions.AllowRecompilingViewsOnFileChange> is explicitly set. |
113 | | - |
114 | | -For guidance and examples of setting the app's compatibility version, see <xref:mvc/compatibility-version>. |
115 | | - |
116 | | -::: moniker-end |
117 | | - |
118 | | -::: moniker range=">= aspnetcore-3.0" |
119 | | - |
120 | 24 | To enable runtime compilation for all environments and configuration modes: |
121 | 25 |
|
122 | 26 | 1. Install the [Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation/) NuGet package. |
@@ -169,27 +73,35 @@ To enable runtime compilation based on the environment and configuration mode: |
169 | 73 | } |
170 | 74 | ``` |
171 | 75 |
|
172 | | -::: moniker-end |
173 | | - |
174 | 76 | ## Additional resources |
175 | 77 |
|
176 | | -::: moniker range="= aspnetcore-1.1" |
177 | | - |
| 78 | +* <xref:razor-pages/index> |
178 | 79 | * <xref:mvc/views/overview> |
| 80 | +* <xref:razor-pages/sdk> |
179 | 81 |
|
180 | 82 | ::: moniker-end |
181 | 83 |
|
182 | | -::: moniker range="= aspnetcore-2.0" |
| 84 | +::: moniker range="< aspnetcore-3.0" |
183 | 85 |
|
184 | | -* <xref:razor-pages/index> |
185 | | -* <xref:mvc/views/overview> |
| 86 | +A Razor file is compiled at runtime, when the associated Razor Page or MVC view is invoked. Razor files are compiled at both build and publish time using the [Razor SDK](xref:razor-pages/sdk). |
186 | 87 |
|
187 | | -::: moniker-end |
| 88 | +## Razor compilation |
| 89 | + |
| 90 | +Build- and publish-time compilation of Razor files is enabled by default by the Razor SDK. Editing Razor files after they're updated is supported at build time. By default, only the compiled *Views.dll* and no *.cshtml* files or references assemblies required to compile Razor files are deployed with your app. |
188 | 91 |
|
189 | | -::: moniker range=">= aspnetcore-2.1" |
| 92 | +> [!IMPORTANT] |
| 93 | +> The precompilation tool has been deprecated, and will be removed in ASP.NET Core 3.0. We recommend migrating to [Razor Sdk](xref:razor-pages/sdk). |
| 94 | +> |
| 95 | +> The Razor SDK is effective only when no precompilation-specific properties are set in the project file. For instance, setting the *.csproj* file's `MvcRazorCompileOnPublish` property to `true` disables the Razor SDK. |
| 96 | + |
| 97 | +## Runtime compilation |
| 98 | + |
| 99 | +Build-time compilation is supplemented by runtime compilation of Razor files. ASP.NET Core MVC will recompile Razor files when the contents of a *.cshtml* file change. |
| 100 | + |
| 101 | +## Additional resources |
190 | 102 |
|
191 | 103 | * <xref:razor-pages/index> |
192 | 104 | * <xref:mvc/views/overview> |
193 | 105 | * <xref:razor-pages/sdk> |
194 | 106 |
|
195 | | -::: moniker-end |
| 107 | +::: moniker-end |
0 commit comments