Events
Power BI DataViz World Championships
Feb 14, 4 PM - Mar 31, 4 PM
With 4 chances to enter, you could win a conference package and make it to the LIVE Grand Finale in Las Vegas
Learn moreThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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 the project.
Note
Runtime 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 while the app is running.
Updating Razor views and Razor Pages during development while the app is running is also supported using .NET Hot Reload.
Note
When enabled, runtime compilation disables .NET Hot Reload. We recommend using Hot Reload instead of Razor runtime compilation during development.
To enable runtime compilation for all environments:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Call AddRazorRuntimeCompilation in Program.cs
:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages()
.AddRazorRuntimeCompilation();
Runtime compilation can be enabled conditionally, which ensures that the published output:
To enable runtime compilation only for the Development environment:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Call AddRazorRuntimeCompilation in Program.cs
when the current environment is set to Development:
var builder = WebApplication.CreateBuilder(args);
var mvcBuilder = builder.Services.AddRazorPages();
if (builder.Environment.IsDevelopment())
{
mvcBuilder.AddRazorRuntimeCompilation();
}
Runtime compilation can also be enabled with a hosting startup assembly. To enable runtime compilation in the Development environment for specific launch profiles:
environmentVariables
section in launchSettings.json
:
Verify that ASPNETCORE_ENVIRONMENT
is set to "Development"
.
Set ASPNETCORE_HOSTINGSTARTUPASSEMBLIES
to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
. For example, the following launchSettings.json
enables runtime compilation for the ViewCompilationSample
and IIS Express
launch profiles:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:7098",
"sslPort": 44332
}
},
"profiles": {
"ViewCompilationSample": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7173;http://localhost:5251",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}
}
}
}
With this approach, no code changes are needed in Program.cs
. At runtime, ASP.NET Core searches for an assembly-level HostingStartup attribute in Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
. The HostingStartup
attribute specifies the app startup code to execute and that startup code enables runtime compilation.
Consider a scenario in which a Razor Pages project references a Razor class library (RCL) named MyClassLib. The RCL contains a _Layout.cshtml
file consumed by MVC and Razor Pages projects. To enable runtime compilation for the _Layout.cshtml
file in that RCL, make the following changes in the Razor Pages project:
Enable runtime compilation with the instructions at Enable runtime compilation conditionally.
Configure MvcRazorRuntimeCompilationOptions in Program.cs
:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
var libraryPath = Path.GetFullPath(
Path.Combine(builder.Environment.ContentRootPath, "..", "MyClassLib"));
options.FileProviders.Add(new PhysicalFileProvider(libraryPath));
});
The preceding code builds an absolute path to the MyClassLib RCL. 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.
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.
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:
To enable runtime compilation for all environments in an existing project:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Update the project's Startup.ConfigureServices
method to include a call to AddRazorRuntimeCompilation. For example:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages()
.AddRazorRuntimeCompilation();
// code omitted for brevity
}
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:
environmentVariables
section in launchSettings.json
:
ASPNETCORE_ENVIRONMENT
is set to "Development"
.ASPNETCORE_HOSTINGSTARTUPASSEMBLIES
to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
.In the following example, runtime compilation is enabled in the Development environment for the IIS Express
and RazorPagesApp
launch profiles:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:57676",
"sslPort": 44364
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}
},
"RazorPagesApp": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}
}
}
}
No code changes are needed in the project's Startup
class. At runtime, ASP.NET Core searches for an assembly-level HostingStartup attribute in Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
. The HostingStartup
attribute specifies the app startup code to execute. That startup code enables runtime compilation.
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:
Enable runtime compilation with the instructions at Conditionally enable runtime compilation in an existing project.
Configure the runtime compilation options in Startup.ConfigureServices
:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
var libraryPath = Path.GetFullPath(
Path.Combine(HostEnvironment.ContentRootPath, "..", "MyClassLib"));
options.FileProviders.Add(new PhysicalFileProvider(libraryPath));
});
}
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.
ASP.NET Core feedback
ASP.NET Core is an open source project. Select a link to provide feedback:
Events
Power BI DataViz World Championships
Feb 14, 4 PM - Mar 31, 4 PM
With 4 chances to enter, you could win a conference package and make it to the LIVE Grand Finale in Las Vegas
Learn moreTraining
Module
Create a web UI with ASP.NET Core - Training
Learn how to create web pages using Razor with ASP.NET Core.
Documentation
Discover how to use partial views to break up large markup files and reduce the duplication of common markup across web pages in ASP.NET Core apps.
Using DisplayTemplates and EditorTemplates
How to use DisplayTemplates and EditorTemplates in ASP.NET Core.
Learn how Areas are an ASP.NET MVC feature used to organize related functionality into a group as a separate namespace (for routing) and folder structure (for views).