Skip to content

Commit 57c6714

Browse files
committed
Merge branch 'feature/Runtime-View-Compilation' into develop
When we migrated to .NET Core 3.0, we lost the ability to have views dynamically compiled at runtime. This is preferable in the production environment, where we don't expect the views to change frequently, as it means that precompiled views are loaded, which is much faster. For development, though, it adds a lot of overhead as it means we need to stop running the debugger, make a change, build the assembly, and then rerun the debugger. This can be mitigated by installing Microsoft's `RuntimeCompilation` package. In addition, this offers support for Razor Class Libraries with the aid of the `FileProviders` option, thus allowing us to change views in the OnTopic Editor and have them (almost) immediately available within the browser window being used for debugging.
2 parents 9b21224 + 23e816c commit 57c6714

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

OnTopic.Editor.AspNetCore.Host/OnTopic.Editor.AspNetCore.Host.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="OnTopic.AspNetCore.Mvc" Version="4.3.0" />
1313
<PackageReference Include="OnTopic.Data.Caching" Version="4.3.0" />
1414
<PackageReference Include="OnTopic.Data.Sql" Version="4.3.0" />
15+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.7" />
1516
</ItemGroup>
1617

1718
<ItemGroup>

OnTopic.Editor.AspNetCore.Host/Startup.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
| Client Ignia, LLC
44
| Project Sample OnTopic Site
55
\=============================================================================================================================*/
6+
using System.IO;
67
using Microsoft.AspNetCore.Builder;
78
using Microsoft.AspNetCore.Hosting;
89
using Microsoft.AspNetCore.Http;
910
using Microsoft.AspNetCore.Mvc.Controllers;
11+
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
1012
using Microsoft.AspNetCore.Mvc.ViewComponents;
1113
using Microsoft.Extensions.Configuration;
1214
using Microsoft.Extensions.DependencyInjection;
15+
using Microsoft.Extensions.FileProviders;
1316
using Microsoft.Extensions.Hosting;
1417
using OnTopic.AspNetCore.Mvc;
1518
using OnTopic.Editor.AspNetCore;
@@ -74,14 +77,25 @@ public void ConfigureServices(IServiceCollection services) {
7477
/*------------------------------------------------------------------------------------------------------------------------
7578
| Configure: MVC
7679
\-----------------------------------------------------------------------------------------------------------------------*/
77-
services.AddControllersWithViews()
80+
var mvcBuilder = services.AddControllersWithViews()
7881

7982
//Add OnTopic support
8083
.AddTopicSupport()
8184

8285
//Add OnTopic editor support
8386
.AddTopicEditor();
8487

88+
/*------------------------------------------------------------------------------------------------------------------------
89+
| Configure: Runtime View Compilation
90+
\-----------------------------------------------------------------------------------------------------------------------*/
91+
if (HostingEnvironment.IsDevelopment()) {
92+
mvcBuilder.AddRazorRuntimeCompilation();
93+
services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
94+
var libraryPath = Path.GetFullPath(Path.Combine(HostingEnvironment.ContentRootPath, "..", "OnTopic.Editor.AspNetCore"));
95+
options.FileProviders.Add(new PhysicalFileProvider(libraryPath));
96+
});
97+
}
98+
8599
/*------------------------------------------------------------------------------------------------------------------------
86100
| Configure: Watch Razor Class Library (RCLs) views
87101
>-------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)