Skip to content

Commit 3d77cdf

Browse files
Razor compilation update (#17684)
* Razor compilation update * Razor compilation update
1 parent 1337bb4 commit 3d77cdf

1 file changed

Lines changed: 19 additions & 107 deletions

File tree

aspnetcore/mvc/views/view-compilation.md

Lines changed: 19 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -2,121 +2,25 @@
22
title: Razor file compilation in ASP.NET Core
33
author: rick-anderson
44
description: Learn how compilation of Razor files occurs in an ASP.NET Core app.
5-
monikerRange: '>= aspnetcore-1.1'
65
ms.author: riande
76
ms.custom: mvc
8-
ms.date: 12/05/2019
7+
ms.date: 4/8/2020
98
uid: mvc/views/view-compilation
109
---
1110
# Razor file compilation in ASP.NET Core
1211

1312
By [Rick Anderson](https://twitter.com/RickAndMSFT)
1413

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-
3314
::: moniker range=">= aspnetcore-3.0"
3415

3516
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.
3617

37-
::: moniker-end
38-
3918
## Razor compilation
4019

41-
::: moniker range=">= aspnetcore-3.0"
42-
4320
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.
4421

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-
![Razor views inside DLL](view-compilation/_static/razor-views-in-dll.png)
94-
95-
::: moniker-end
96-
9722
## Runtime compilation
9823

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-
12024
To enable runtime compilation for all environments and configuration modes:
12125

12226
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:
16973
}
17074
```
17175

172-
::: moniker-end
173-
17476
## Additional resources
17577

176-
::: moniker range="= aspnetcore-1.1"
177-
78+
* <xref:razor-pages/index>
17879
* <xref:mvc/views/overview>
80+
* <xref:razor-pages/sdk>
17981

18082
::: moniker-end
18183

182-
::: moniker range="= aspnetcore-2.0"
84+
::: moniker range="< aspnetcore-3.0"
18385

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).
18687

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.
18891

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
190102

191103
* <xref:razor-pages/index>
192104
* <xref:mvc/views/overview>
193105
* <xref:razor-pages/sdk>
194106

195-
::: moniker-end
107+
::: moniker-end

0 commit comments

Comments
 (0)