aspnetcore: [Discussion] Breaking changes to runtime compilation for Razor views and Razor Pages

As a consequence of cleaning up the ASP.NET Core shared framework to not depend on Roslyn, support for runtime compilation of Razor views and Razor Pages is being moved to a separate package. Applications that require runtime compilation or re-compilation of Razor files should

  • Add a reference to the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package
  • Update the application’s ConfigureServices to include a call to AddMvcRazorRuntimeCompilation:
services.AddMvc()
    .AddMvcRazorRuntimeCompilation();

The following APIs previously available on Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions to support runtime compilation would now be available via Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.MvcRazorRuntimeCompilationOptions:

  • RazorViewEngineOptions.FileProviders -> MvcRazorRuntimeCompilationOptions.FileProviders
  • RazorViewEngineOptions.AdditionalCompilationReferences -> MvcRazorRuntimeCompilationOptions.AdditionalReferencePaths

In addition, Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions.AllowRecompilingViewsOnFileChange has been removed. Recompilation on file changes is enabled by default by referencing the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package.

Related issues: https://github.com/aspnet/Announcements/issues/312, https://github.com/aspnet/Announcements/issues/325

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 26 (8 by maintainers)

Most upvoted comments

@pixeldm Maybe you could make that package reference conditional.

In your csproj

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <DefineConstants>UseRazorCompilation</DefineConstants>
</PropertyGroup>

Then in your startup, use pre processor directive

#if UseRazorCompilation .AddMvcRazorRuntimeCompilation(); #endif

And also in your csproj add that condition to the package reference!

Is there any way to set AddMvcRazorRuntimeCompilation() for only the dev environment? Currently, the only way to get design time recompiling of a view is to turn this feature on. Otherwise, the project must be restarted to see any changes to a view. However, enabling this feature also involves adding the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package. This package adds a considerable amount of files to the publish folder when publishing. Furthermore, I don’t really want runtime compilation of views for production. Currently, I can’t find any way to limit this feature to just development. Any suggestions?

  1. Install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
  2. Modify services.AddRazorPages() to services.AddRazorPages().AddRazorRuntimeCompilation();

@mqudsi It’ll be available as part of the 3.0.0-preview3 release.

(This definitely deserves to be in the migration docs for the 3.0 jump.)

Any word on when the package will be available via nuget?

EDIT: I accidentally a word.

I’m pretty sure the time to discuss not going this route that has long since sailed. These comments go out to everyone that’s subscribed to the thread, so please try to avoid tangential discussions. If you feel strongly about it, you should probably open a new issue.

Any chance the dev team can undo whatever they did that broke this? I’ve added the package reference and I too have concerns with the sheer number of additional files required to implement this fix as well as the speed difference when making a change and simply refreshing vs 2.2’s speed and ability to recompile the view.

From the OP and announcement:

Recompilation on file changes is enabled by default by referencing the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package

Yet the docs and other posts here indicate you also need to set:

services.AddRazorRuntimeCompilation() - would be worth updating the announcement and OP here if this is indeed needed.

After adding a reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation I get a version mismatch on the Microsoft.CodeAnalysis.Common package. This is because Microsoft.VisualStudio.Web.CodeGeneration.Design is referencing an older version. I had to remove the reference to Microsoft.VisualStudio.Web.CodeGeneration.Design otherwise view recompilation was throwing an exception.