Skip to content

Commit b61139b

Browse files
Razor file compilation (#17709)
* Razor file compilation * Razor file compilation * Razor file compilation * Razor file compilation * Apply suggestions from code review Co-Authored-By: Scott Addie <10702007+scottaddie@users.noreply.github.com> * Razor file compilation * Razor file compilation Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com>
1 parent 208f824 commit b61139b

2 files changed

Lines changed: 61 additions & 19 deletions

File tree

aspnetcore/mvc/views/view-compilation.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To enable runtime compilation for all environments and configuration modes:
2525

2626
1. Install the [Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation/) NuGet package.
2727

28-
1. Update the project's `Startup.ConfigureServices` method to include a call to `AddRazorRuntimeCompilation`. For example:
28+
1. Update the project's `Startup.ConfigureServices` method to include a call to <xref:Microsoft.Extensions.DependencyInjection.RazorRuntimeCompilationMvcBuilderExtensions.AddRazorRuntimeCompilation*>. For example:
2929

3030
```csharp
3131
public void ConfigureServices(IServiceCollection services)
@@ -55,29 +55,15 @@ To enable runtime compilation based on the environment and configuration mode:
5555

5656
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`:
5757

58-
```csharp
59-
public IWebHostEnvironment Env { get; set; }
60-
61-
public void ConfigureServices(IServiceCollection services)
62-
{
63-
IMvcBuilder builder = services.AddRazorPages();
64-
65-
#if DEBUG
66-
if (Env.IsDevelopment())
67-
{
68-
builder.AddRazorRuntimeCompilation();
69-
}
70-
#endif
71-
72-
// code omitted for brevity
73-
}
74-
```
58+
[!code-csharp[](~/mvc/views/view-compilation/sample/Startup.cs?name=snippet)]
7559

7660
## Additional resources
7761

62+
* [RazorCompileOnBuild and RazorCompileOnPublish](xref:razor-pages/sdk#properties) properties.
7863
* <xref:razor-pages/index>
7964
* <xref:mvc/views/overview>
8065
* <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.
8167
8268
::: moniker-end
8369

@@ -104,4 +90,4 @@ Build-time compilation is supplemented by runtime compilation of Razor files. AS
10490
* <xref:mvc/views/overview>
10591
* <xref:razor-pages/sdk>
10692

107-
::: moniker-end
93+
::: moniker-end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
7+
#region snippet
8+
public class Startup
9+
{
10+
public Startup(IConfiguration configuration, IWebHostEnvironment env)
11+
{
12+
Configuration = configuration;
13+
Env = env;
14+
}
15+
16+
public IWebHostEnvironment Env { get; set; }
17+
public IConfiguration Configuration { get; }
18+
19+
public void ConfigureServices(IServiceCollection services)
20+
{
21+
IMvcBuilder builder = services.AddRazorPages();
22+
23+
#if DEBUG
24+
if (Env.IsDevelopment())
25+
{
26+
builder.AddRazorRuntimeCompilation();
27+
}
28+
#endif
29+
}
30+
31+
public void Configure(IApplicationBuilder app)
32+
{
33+
if (Env.IsDevelopment())
34+
{
35+
app.UseDeveloperExceptionPage();
36+
}
37+
else
38+
{
39+
app.UseExceptionHandler("/Error");
40+
app.UseHsts();
41+
}
42+
43+
app.UseHttpsRedirection();
44+
app.UseStaticFiles();
45+
46+
app.UseRouting();
47+
48+
app.UseAuthorization();
49+
50+
app.UseEndpoints(endpoints =>
51+
{
52+
endpoints.MapRazorPages();
53+
});
54+
}
55+
}
56+
#endregion

0 commit comments

Comments
 (0)