Enable razor runtime compilation что это
AspNet Core 3.0 and 3.1 : Enable runtime compilation for Razor Pages
Since ASP.Net Core version 3.0 and higher:
A) Editing a Razor View (.cshtml) file while running the application does not apply the changes until restart.
B) Looks like edit and continue is not working.
IDE and Version: Microsoft Visual Studio 2019
4 Answers 4
For this issue, I suggest you try to install package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation and then configure AddRazorRuntimeCompilation in Startup.cs like
Please use NuGet package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation Version 3.1.7 that is compatible with ASP.Net Core 3.1 and apply following line of code in Startup.cs
Note as indicated in the «Razor file compilation in ASP.NET Core» posted by Givi, if you just want this feature for rapid development, you can enable this just for development and not for production by NOT adding
to Startup.cs, and instead adding these environment variables to your launchSettings.json
only for our integrated development environment (Dev appserver deployed through DevOps) and use
for individual developer’s local development (in an accompanying appsettings.Local.json config file).
I’m happy to report that «ASPNETCORE_ENVIRONMENT» does not strictly need to be set to «Development» and that runtime compilation works for «Local» (and presumably whatever is the name you use for the environment you develop under).
Razor file compilation in ASP.NET Core
Razor files with a .cshtml extension are compiled at both build and publish time using the Razor SDK. Runtime compilation may be optionally enabled by configuring your project.
Razor compilation
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.
Enable runtime compilation at project creation
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.
In the Create a new ASP.NET Core web application dialog:
Enable runtime compilation in an existing project
To enable runtime compilation for all environments in an existing project:
Update the project’s Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation. For example:
Conditionally enable runtime compilation in an existing project
Runtime compilation can be enabled such that it’s only available for local development. Conditionally enabling in this manner ensures that the published output:
To enable runtime compilation only in the Development environment:
In the following example, runtime compilation is enabled in the Development environment for the IIS Express and RazorPagesApp launch profiles:
Enable runtime compilation for a Razor Class Library
Consider a scenario in which a Razor Pages project references a Razor Class Library (RCL) 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:
Configure the runtime compilation options in Startup.ConfigureServices :
In the preceding code, an absolute path to the MyClassLib RCL is constructed. The PhysicalFileProvider API 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.
Additional resources
Razor files with a .cshtml extension are compiled at both build and publish time using the Razor SDK. Runtime compilation may be optionally enabled by configuring your project.
Razor compilation
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.
Enable runtime compilation at project creation
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.
In the Create a new ASP.NET Core web application dialog:
Enable runtime compilation in an existing project
To enable runtime compilation for all environments in an existing project:
Update the project’s Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation. For example:
Conditionally enable runtime compilation in an existing project
Runtime compilation can be enabled such that it’s only available for local development. Conditionally enabling in this manner ensures that the published output:
To enable runtime compilation only in the Development environment:
In the following example, runtime compilation is enabled in the Development environment for the IIS Express and RazorPagesApp launch profiles:
Enable runtime compilation for a Razor Class Library
Consider a scenario in which a Razor Pages project references a Razor Class Library (RCL) 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:
Configure the runtime compilation options in Startup.ConfigureServices :
In the preceding code, an absolute path to the MyClassLib RCL is constructed. The PhysicalFileProvider API 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.
Additional resources
A Razor file is compiled at runtime, when the associated Razor Page or MVC view is invoked. Razor files are compiled at both build and publish time using the Razor SDK.
Razor compilation
Build-time and publish-time compilation of Razor files is enabled by default by the Razor SDK. Editing Razor files after they’re updated is supported at build time. By default, only the compiled Views.dll and no .cshtml files or references assemblies required to compile Razor files are deployed with an app.
The precompilation tool has been deprecated, and will be removed in ASP.NET Core 3.0. We recommend migrating to Razor Sdk.
The Razor SDK is effective only when no precompilation-specific properties are set in the project file. For instance, setting the .csproj file’s MvcRazorCompileOnPublish property to true disables the Razor SDK.
Add Razor runtime compilation when developing ASP.NET Core
When I started looking into ASP.NET Core, I was a bit surprised that I had to compile my web project in order to publish changes in cshtml Razor files. Runtime compilation of Razor files just worked out of the box with ASP.NET MVC. In this post, I’ll show you how we solved this when migrating the elmah.io app to ASP.NET Core.
Microsoft wrote some good documentation on how to enable runtime compilation of Razor files in ASP.NET Core. You can even click a checkbox when creating a new (> ASP.NET Core 3.1) project. I like to have my main configuration in strongly-typed C# with the benefits of having IntelliSense, which isn’t the case with the solution provided as part of the Wizard.
To set up runtime compilation, start by installing the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package:
In the ConfigureServices method in the Startup.cs file, call the AddRazorRuntimeCompilation method. Since we only update Razor files during development and not in production (right?), wrap the call in an if statement like this:
The IsDevelopment call ensure that runtime compilation is only added during development. The Env property should be injected in the constructor if not already there:
An additional benefit of configuring this in C# is the possibility to include options. Like if you prefer to have Razor files in custom directories:
.NET Core 3.0: Razor views don’t automatically recompile on change
According to the documentation, Razor views should, by default, recompile on change on local environments for ASP.NET Core 3.0.
However, my project doesn’t do this locally. If I change a view and refresh when I’m debugging locally, the change is not reflected. I have to stop the solution, re-run, and then see the change.
I am doing this on a default ASP.NET Core Web Application template on Visual Studio 2019 with ASP.NET Core 3.0.0 Preview 2 using Razor pages. Any idea if I need to change settings to enable this feature?
UPDATE NOV 2019 FOR 3.0 FULL RELEASE:
This question still gets a lot of views. A few answers have cited to add
9 Answers 9
For ASP.NET Core 3 release version:
It can also be enabled conditionally only for local development, quoted from the link:
Runtime compilation can be enabled such that it’s only available for local development. Conditionally enabling in this manner ensures that the published output:
Uses compiled views.
Is smaller in size.
Doesn’t enable file watchers in production.
OK it looks like it’s not supported yet 🙁
Runtime compilation removed As a consequence of cleaning up the ASP.NET Core shared framework to not depend on Roslyn, support for runtime compilation of pages and views has also been removed in this preview release. Instead compilation of pages and views is performed at build time. In a future preview update we will provide a NuGet packages for optionally enabling runtime compilation support in an app.
Applications that require runtime compilation or re-compilation of Razor files should:
Enable razor runtime compilation что это
Razor file compilation in ASP.NET Core
. moniker range=»>= aspnetcore-6.0″
Razor files with a .cshtml extension are compiled at both build and publish time using the Razor SDK. Runtime compilation may be optionally enabled by configuring your project.
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.
Enable runtime compilation at project creation
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.
In the Create a new ASP.NET Core web application dialog:
Enable runtime compilation in an existing project
To enable runtime compilation for all environments in an existing project:
Update the project’s Startup.ConfigureServices method to include a call to xref:Microsoft.Extensions.DependencyInjection.RazorRuntimeCompilationMvcBuilderExtensions.AddRazorRuntimeCompilation*. For example:
Conditionally enable runtime compilation in an existing project
Runtime compilation can be enabled such that it’s only available for local development. Conditionally enabling in this manner ensures that the published output:
To enable runtime compilation only in the Development environment:
In the following example, runtime compilation is enabled in the Development environment for the IIS Express and RazorPagesApp launch profiles:
Enable runtime compilation for a Razor Class Library
Consider a scenario in which a Razor Pages project references a Razor Class Library (RCL) 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:
Configure the runtime compilation options in Startup.ConfigureServices :
In the preceding code, an absolute path to the MyClassLib RCL is constructed. The PhysicalFileProvider API 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.
. moniker range=»>= aspnetcore-3.0
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.
Enable runtime compilation at project creation
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.
In the Create a new ASP.NET Core web application dialog:
Enable runtime compilation in an existing project
To enable runtime compilation for all environments in an existing project:
Update the project’s Startup.ConfigureServices method to include a call to xref:Microsoft.Extensions.DependencyInjection.RazorRuntimeCompilationMvcBuilderExtensions.AddRazorRuntimeCompilation*. For example:
Conditionally enable runtime compilation in an existing project
Runtime compilation can be enabled such that it’s only available for local development. Conditionally enabling in this manner ensures that the published output:
To enable runtime compilation only in the Development environment:
In the following example, runtime compilation is enabled in the Development environment for the IIS Express and RazorPagesApp launch profiles:
Enable runtime compilation for a Razor Class Library
Consider a scenario in which a Razor Pages project references a Razor Class Library (RCL) 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:
Configure the runtime compilation options in Startup.ConfigureServices :
In the preceding code, an absolute path to the MyClassLib RCL is constructed. The PhysicalFileProvider API 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.
Build-time and publish-time compilation of Razor files is enabled by default by the Razor SDK. Editing Razor files after they’re updated is supported at build time. By default, only the compiled Views.dll and no .cshtml files or references assemblies required to compile Razor files are deployed with an app.
[!IMPORTANT] The precompilation tool has been deprecated, and will be removed in ASP.NET Core 3.0. We recommend migrating to Razor Sdk.
The Razor SDK is effective only when no precompilation-specific properties are set in the project file. For instance, setting the .csproj file’s MvcRazorCompileOnPublish property to true disables the Razor SDK.