Skip to content

Commit 9f9bd9a

Browse files
scottaddieserpent5Rick-Anderson
authored
Adapt Razor runtime compilation guidance to changes made in ASP.NET Core 3.1 (#17777)
* Adapt Razor runtime compilation guidance to changes made in 3.1 * Remove Runtime compilation heading * Update line highlighting * Verbiage tweak * Restore 3.0 content * minor edits * Clarify instructions * Fix indentation * Expand explanation * Fix xref link * Apply suggestions from code review Co-Authored-By: Kirk Larkin <6025110+serpent5@users.noreply.github.com> * Apply suggestions from code review Co-Authored-By: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * React to feedback Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>
1 parent 2239b40 commit 9f9bd9a

7 files changed

Lines changed: 112 additions & 46 deletions

File tree

aspnetcore/mvc/views/view-compilation.md

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,96 @@ author: rick-anderson
44
description: Learn how compilation of Razor files occurs in an ASP.NET Core app.
55
ms.author: riande
66
ms.custom: mvc
7-
ms.date: 4/8/2020
7+
ms.date: 04/13/2020
88
uid: mvc/views/view-compilation
99
---
1010
# Razor file compilation in ASP.NET Core
1111

1212
By [Rick Anderson](https://twitter.com/RickAndMSFT)
1313

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:
49+
50+
```csharp
51+
public void ConfigureServices(IServiceCollection services)
52+
{
53+
services.AddRazorPages()
54+
.AddRazorRuntimeCompilation();
55+
56+
// code omitted for brevity
57+
}
58+
```
59+
60+
## Conditionally enable runtime compilation in an existing project
61+
62+
Runtime compilation can be enabled such that it's only available for local development. Conditionally enabling in this manner ensures that the published output:
63+
64+
* Uses compiled views.
65+
* Doesn't enable file watchers in production.
66+
67+
To enable runtime compilation only in the Development environment:
68+
69+
1. Install the [Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation/) NuGet package.
70+
1. Modify the launch profile `environmentVariables` section in *launchSettings.json*:
71+
* Verify `ASPNETCORE_ENVIRONMENT` is set to `"Development"`.
72+
* Set `ASPNETCORE_HOSTINGSTARTUPASSEMBLIES` to `"Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"`.
73+
74+
In the following example, runtime compilation is enabled in the Development environment for the `IIS Express` and `RazorPagesApp` launch profiles:
75+
76+
[!code-json[](~/mvc/views/view-compilation/samples/3.1/launchSettings.json?highlight=15-16,24-25)]
77+
78+
No code changes are needed in the project'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.
79+
80+
## Additional resources
81+
82+
* [RazorCompileOnBuild and RazorCompileOnPublish](xref:razor-pages/sdk#properties) properties.
83+
* <xref:razor-pages/index>
84+
* <xref:mvc/views/overview>
85+
* <xref:razor-pages/sdk>
86+
* See the [runtime compilation sample on GitHub](https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation) for a sample that shows making runtime compilation work across projects.
87+
88+
::: moniker-end
89+
90+
::: moniker range="= aspnetcore-3.0"
1591

1692
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.
1793

1894
## Razor compilation
1995

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.
96+
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.
2197

2298
## Runtime compilation
2399

@@ -55,15 +131,15 @@ To enable runtime compilation based on the environment and configuration mode:
55131

56132
1. Update the project'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`:
57133

58-
[!code-csharp[](~/mvc/views/view-compilation/sample/Startup.cs?name=snippet)]
134+
[!code-csharp[](~/mvc/views/view-compilation/samples/3.0/Startup.cs?name=snippet)]
59135

60136
## Additional resources
61137

62138
* [RazorCompileOnBuild and RazorCompileOnPublish](xref:razor-pages/sdk#properties) properties.
63139
* <xref:razor-pages/index>
64140
* <xref:mvc/views/overview>
65141
* <xref:razor-pages/sdk>
66-
* See the [runtimecompilation sample on GitHub](https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation) for a sample that shows making runtime compilation work across projects.
142+
* See the [runtime compilation sample on GitHub](https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation) for a sample that shows making runtime compilation work across projects.
67143
68144
::: moniker-end
69145

Binary file not shown.

aspnetcore/mvc/views/view-compilation/sample/DotNetFrameworkProject.csproj

Lines changed: 0 additions & 19 deletions
This file was deleted.

aspnetcore/mvc/views/view-compilation/sample/MvcRazorCompileOnPublish.csproj

Lines changed: 0 additions & 12 deletions
This file was deleted.

aspnetcore/mvc/views/view-compilation/sample/RuntimeCompilation.csproj

Lines changed: 0 additions & 9 deletions
This file was deleted.

aspnetcore/mvc/views/view-compilation/sample/Startup.cs renamed to aspnetcore/mvc/views/view-compilation/samples/3.0/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ public void Configure(IApplicationBuilder app)
5353
});
5454
}
5555
}
56-
#endregion
56+
#endregion
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:57676",
7+
"sslPort": 44364
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development",
16+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
17+
}
18+
},
19+
"RazorPagesApp": {
20+
"commandName": "Project",
21+
"launchBrowser": true,
22+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development",
25+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
26+
}
27+
}
28+
}
29+
}
30+

0 commit comments

Comments
 (0)