Skip to content

Commit 9a66c86

Browse files
Explain how to enable Razor Class Library runtime compilation in ASP.NET Core 3.1+ (#17792)
* Explain how to enable RCL runtime compilation * more work * more work * Update aspnetcore/mvc/views/view-compilation.md Co-Authored-By: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>
1 parent e01f51d commit 9a66c86

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

aspnetcore/mvc/views/view-compilation.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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: 04/13/2020
7+
ms.date: 04/14/2020
88
uid: mvc/views/view-compilation
99
---
1010
# Razor file compilation in ASP.NET Core
@@ -77,13 +77,23 @@ In the following example, runtime compilation is enabled in the Development envi
7777

7878
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.
7979

80+
## Enable runtime compilation for a Razor Class Library
81+
82+
Consider a scenario in which a Razor Pages project references a [Razor Class Library (RCL)](xref:razor-pages/ui-class) named *MyClassLib*. The RCL contains a *_Layout.cshtml* file that all of your team's MVC and Razor Pages projects consume. You want to enable runtime compilation for the *_Layout.cshtml* file in that RCL. Make the following changes in the Razor Pages project:
83+
84+
1. Enable runtime compilation with the instructions at [Conditionally enable runtime compilation in an existing project](#conditionally-enable-runtime-compilation-in-an-existing-project).
85+
1. Configure the runtime compilation options in `Startup.ConfigureServices`:
86+
87+
[!code-csharp[](~/mvc/views/view-compilation/samples/3.1/Startup.cs?name=snippet_ConfigureServices&highlight=5-10)]
88+
89+
In the preceding code, an absolute path to the *MyClassLib* RCL is constructed. The [PhysicalFileProvider API](xref:fundamentals/file-providers#physicalfileprovider) is used to locate directories and files at that absolute path. Finally, the `PhysicalFileProvider` instance is added to a file providers collection, which allows access to the RCL's *.cshtml* files.
90+
8091
## Additional resources
8192

8293
* [RazorCompileOnBuild and RazorCompileOnPublish](xref:razor-pages/sdk#properties) properties.
8394
* <xref:razor-pages/index>
8495
* <xref:mvc/views/overview>
8596
* <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.
8797

8898
::: moniker-end
8999

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.IO;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.FileProviders;
8+
using Microsoft.Extensions.Hosting;
9+
10+
namespace MyApp
11+
{
12+
public class Startup
13+
{
14+
public Startup(IConfiguration configuration, IWebHostEnvironment env)
15+
{
16+
Configuration = configuration;
17+
HostEnvironment = env;
18+
}
19+
20+
public IConfiguration Configuration { get; }
21+
22+
public IWebHostEnvironment HostEnvironment { get; }
23+
24+
#region snippet_ConfigureServices
25+
public void ConfigureServices(IServiceCollection services)
26+
{
27+
services.AddRazorPages();
28+
29+
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
30+
{
31+
var libraryPath = Path.GetFullPath(
32+
Path.Combine(HostEnvironment.ContentRootPath, "..", "MyClassLib"));
33+
options.FileProviders.Add(new PhysicalFileProvider(libraryPath));
34+
});
35+
}
36+
#endregion
37+
38+
public void Configure(IApplicationBuilder app)
39+
{
40+
if (HostEnvironment.IsDevelopment())
41+
{
42+
app.UseDeveloperExceptionPage();
43+
}
44+
else
45+
{
46+
app.UseExceptionHandler("/Home/Error");
47+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
48+
app.UseHsts();
49+
}
50+
app.UseHttpsRedirection();
51+
app.UseStaticFiles();
52+
53+
app.UseRouting();
54+
55+
app.UseAuthorization();
56+
57+
app.UseEndpoints(endpoints =>
58+
{
59+
endpoints.MapControllerRoute(
60+
name: "default",
61+
pattern: "{controller=Home}/{action=Index}/{id?}");
62+
});
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)