Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Overview
The .NET 6 SDK includes the Microsoft.NET.Sdk.Razor MSBuild SDK (Razor SDK). The Razor SDK:
- Is required to build, package, and publish projects containing Razor files for ASP.NET Core MVC-based or Blazor projects.
- Includes a set of predefined properties, and items that allow customizing the compilation of Razor (.cshtmlor.razor) files.
The Razor SDK includes Content items with Include attributes set to the **\*.cshtml and **\*.razor globbing patterns. Matching files are published.
Prerequisites
Use the Razor SDK
Most web apps aren't required to explicitly reference the Razor SDK.
To use the Razor SDK to build class libraries containing Razor views or Razor Pages, we recommend starting with the Razor class library (RCL) project template. An RCL that's used to build Blazor (.razor) files minimally requires a reference to the Microsoft.AspNetCore.Components package. An RCL that's used to build Razor views or pages (.cshtml files) minimally requires targeting netcoreapp3.0 or later and has a FrameworkReference to the Microsoft.AspNetCore.App metapackage in its project file.
Properties
The following properties control the Razor's SDK behavior as part of a project build:
- RazorCompileOnBuild: When- true, compiles and emits the Razor assembly as part of building the project. Defaults to- true.
- RazorCompileOnPublish: When- true, compiles and emits the Razor assembly as part of publishing the project. Defaults to- true.
- UseRazorSourceGenerator: Defaults to- true. When- true:- Compiles using source generation.
- Doesn't create <app_name>.Views.dll. Views are included in<app_name>.dll.
- Supports .NET Hot Reload.
 
The properties and items in the following table are used to configure inputs and output to the Razor SDK.
| Items | Description | 
|---|---|
| RazorGenerate | Item elements ( .cshtmlfiles) that are inputs to code generation. | 
| RazorComponent | Item elements ( .razorfiles) that are inputs to Razor component code generation. | 
| RazorCompile | Item elements ( .csfiles) that are inputs to Razor compilation targets. Use thisItemGroupto specify additional files to be compiled into the Razor assembly. | 
| RazorEmbeddedResource | Item elements added as embedded resources to the generated Razor assembly. | 
| Property | Description | 
|---|---|
| RazorOutputPath | The Razor output directory. | 
| RazorCompileToolset | Used to determine the toolset used to build the Razor assembly. Valid values are Implicit,RazorSDK, andPrecompilationTool. | 
| EnableDefaultContentItems | Default is true. Whentrue, includes web.config,.json, and.cshtmlfiles as content in the project. When referenced viaMicrosoft.NET.Sdk.Web, files under wwwroot and config files are also included. | 
| EnableDefaultRazorGenerateItems | When true, includes.cshtmlfiles fromContentitems inRazorGenerateitems. | 
| GenerateRazorTargetAssemblyInfo | Not used in .NET 6 or later. | 
| EnableDefaultRazorTargetAssemblyInfoAttributes | Not used in .NET 6 or later. | 
| CopyRazorGenerateFilesToPublishDirectory | When true, copiesRazorGenerateitems (.cshtml) files to the publish directory. Typically, Razor files aren't required for a published app if they participate in compilation at build-time or publish-time. Defaults tofalse. | 
| PreserveCompilationReferences | When true, copy reference assembly items to the publish directory. Typically, reference assemblies aren't required for a published app if Razor compilation occurs at build-time or publish-time. Set totrueif your published app requires runtime compilation. For example, set the value totrueif the app modifies.cshtmlfiles at runtime or uses embedded views. Defaults tofalse. | 
| IncludeRazorContentInPack | When true, all Razor content items (.cshtmlfiles) are marked for inclusion in the generated NuGet package. Defaults tofalse. | 
| EmbedRazorGenerateSources | When true, adds RazorGenerate (.cshtml) items as embedded files to the generated Razor assembly. Defaults tofalse. | 
| GenerateMvcApplicationPartsAssemblyAttributes | Not used in .NET 6 or later. | 
| DefaultWebContentItemExcludes | A globbing pattern for item elements that are to be excluded from the Contentitem group in projects targeting the Web or Razor SDK | 
| ExcludeConfigFilesFromBuildOutput | When true, .config and.jsonfiles do not get copied to the build output directory. | 
| AddRazorSupportForMvc | When true, configures the Razor SDK to add support for the MVC configuration that is required when building applications containing MVC views or Razor Pages. This property is implicitly set for .NET Core 3.0 or later projects targeting the Web SDK | 
| RazorLangVersion | The version of the Razor Language to target. | 
| EmitCompilerGeneratedFiles | When set to true, the generated source files are written to disk. Setting totrueis useful when debugging the compiler. The default isfalse. | 
For more information on properties, see MSBuild properties.
Runtime compilation of Razor views
- By default, the Razor SDK doesn't publish reference assemblies that are required to perform runtime compilation. This results in compilation failures when the application model relies on runtime compilation—for example, the app uses embedded views or changes views after the app is published. Set - CopyRefAssembliesToPublishDirectoryto- trueto continue publishing reference assemblies. Both code generation and compilation are supported by a single call to the compiler. A single assembly is produced that contains the app types and the generated views.
- For a web app, ensure your app is targeting the - Microsoft.NET.Sdk.WebSDK.
Razor language version
When targeting the Microsoft.NET.Sdk.Web SDK, the Razor language version is inferred from the app's target framework version. For projects targeting the Microsoft.NET.Sdk.Razor SDK or in the rare case that the app requires a different Razor language version than the inferred value, a version can be configured by setting the <RazorLangVersion> property in the app's project file:
<PropertyGroup>
  <RazorLangVersion>{VERSION}</RazorLangVersion>
</PropertyGroup>
Razor's language version is tightly integrated with the version of the runtime that it was built for. Targeting a language version that isn't designed for the runtime is unsupported and likely produces build errors.
Additional resources
The .NET Core 2.1 or later SDK includes the Microsoft.NET.Sdk.Razor MSBuild SDK (Razor SDK). The Razor SDK:
- Is required to build, package, and publish projects containing Razor files for ASP.NET Core MVC-based or Blazor projects.
- Includes a set of predefined targets, properties, and items that allow customizing the compilation of Razor (.cshtmlor.razor) files.
The Razor SDK includes Content items with Include attributes set to the **\*.cshtml and **\*.razor globbing patterns. Matching files are published.
Prerequisites
Use the Razor SDK
Most web apps aren't required to explicitly reference the Razor SDK.
To use the Razor SDK to build class libraries containing Razor views or Razor Pages, we recommend starting with the Razor class library (RCL) project template. An RCL that's used to build Blazor (.razor) files minimally requires a reference to the Microsoft.AspNetCore.Components package. An RCL that's used to build Razor views or pages (.cshtml files) minimally requires targeting netcoreapp3.0 or later and has a FrameworkReference to the Microsoft.AspNetCore.App metapackage in its project file.
Properties
The following properties control the Razor's SDK behavior as part of a project build:
- RazorCompileOnBuild: When- true, compiles and emits the Razor assembly as part of building the project. Defaults to- true.
- RazorCompileOnPublish: When- true, compiles and emits the Razor assembly as part of publishing the project. Defaults to- true.
The properties and items in the following table are used to configure inputs and output to the Razor SDK.
Warning
Starting with ASP.NET Core 3.0, MVC Views or Razor Pages aren't served by default if the RazorCompileOnBuild or RazorCompileOnPublish MSBuild properties in the project file are disabled. Applications must add an explicit reference to the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package if the app relies on runtime compilation to process .cshtml files.
| Items | Description | 
|---|---|
| RazorGenerate | Item elements ( .cshtmlfiles) that are inputs to code generation. | 
| RazorComponent | Item elements ( .razorfiles) that are inputs to Razor component code generation. | 
| RazorCompile | Item elements ( .csfiles) that are inputs to Razor compilation targets. Use thisItemGroupto specify additional files to be compiled into the Razor assembly. | 
| RazorTargetAssemblyAttribute | Item elements used to code generate attributes for the Razor assembly. For example: RazorAssemblyAttributeInclude="System.Reflection.AssemblyMetadataAttribute"_Parameter1="BuildSource" _Parameter2="https://free.blessedness.top/"> | 
| RazorEmbeddedResource | Item elements added as embedded resources to the generated Razor assembly. | 
| Property | Description | 
|---|---|
| RazorTargetName | File name (without extension) of the assembly produced by Razor. | 
| RazorOutputPath | The Razor output directory. | 
| RazorCompileToolset | Used to determine the toolset used to build the Razor assembly. Valid values are Implicit,RazorSDK, andPrecompilationTool. | 
| EnableDefaultContentItems | Default is true. Whentrue, includes web.config,.json, and.cshtmlfiles as content in the project. When referenced viaMicrosoft.NET.Sdk.Web, files under wwwroot and config files are also included. | 
| EnableDefaultRazorGenerateItems | When true, includes.cshtmlfiles fromContentitems inRazorGenerateitems. | 
| GenerateRazorTargetAssemblyInfo | When true, generates a.csfile containing attributes specified byRazorAssemblyAttributeand includes the file in the compile output. | 
| EnableDefaultRazorTargetAssemblyInfoAttributes | When true, adds a default set of assembly attributes toRazorAssemblyAttribute. | 
| CopyRazorGenerateFilesToPublishDirectory | When true, copiesRazorGenerateitems (.cshtml) files to the publish directory. Typically, Razor files aren't required for a published app if they participate in compilation at build-time or publish-time. Defaults tofalse. | 
| PreserveCompilationReferences | When true, copy reference assembly items to the publish directory. Typically, reference assemblies aren't required for a published app if Razor compilation occurs at build-time or publish-time. Set totrueif your published app requires runtime compilation. For example, set the value totrueif the app modifies.cshtmlfiles at runtime or uses embedded views. Defaults tofalse. | 
| IncludeRazorContentInPack | When true, all Razor content items (.cshtmlfiles) are marked for inclusion in the generated NuGet package. Defaults tofalse. | 
| EmbedRazorGenerateSources | When true, adds RazorGenerate (.cshtml) items as embedded files to the generated Razor assembly. Defaults tofalse. | 
| UseRazorBuildServer | When true, uses a persistent build server process to offload code generation work. Defaults to the value ofUseSharedCompilation. | 
| GenerateMvcApplicationPartsAssemblyAttributes | When true, the SDK generates additional attributes used by MVC at runtime to perform application part discovery. | 
| DefaultWebContentItemExcludes | A globbing pattern for item elements that are to be excluded from the Contentitem group in projects targeting the Web or Razor SDK | 
| ExcludeConfigFilesFromBuildOutput | When true, .config and.jsonfiles do not get copied to the build output directory. | 
| AddRazorSupportForMvc | When true, configures the Razor SDK to add support for the MVC configuration that is required when building applications containing MVC views or Razor Pages. This property is implicitly set for .NET Core 3.0 or later projects targeting the Web SDK | 
| RazorLangVersion | The version of the Razor Language to target. | 
For more information on properties, see MSBuild properties.
Targets
The Razor SDK defines two primary targets:
- RazorGenerate: Code generates- .csfiles from- RazorGenerateitem elements. Use the- RazorGenerateDependsOnproperty to specify additional targets that can run before or after this target.
- RazorCompile: Compiles generated- .csfiles in to a Razor assembly. Use the- RazorCompileDependsOnto specify additional targets that can run before or after this target.
- RazorComponentGenerate: Code generates- .csfiles for- RazorComponentitem elements. Use the- RazorComponentGenerateDependsOnproperty to specify additional targets that can run before or after this target.
Runtime compilation of Razor views
- By default, the Razor SDK doesn't publish reference assemblies that are required to perform runtime compilation. This results in compilation failures when the application model relies on runtime compilation—for example, the app uses embedded views or changes views after the app is published. Set - CopyRefAssembliesToPublishDirectoryto- trueto continue publishing reference assemblies.
- For a web app, ensure your app is targeting the - Microsoft.NET.Sdk.WebSDK.
Razor language version
When targeting the Microsoft.NET.Sdk.Web SDK, the Razor language version is inferred from the app's target framework version. For projects targeting the Microsoft.NET.Sdk.Razor SDK or in the rare case that the app requires a different Razor language version than the inferred value, a version can be configured by setting the <RazorLangVersion> property in the app's project file:
<PropertyGroup>
  <RazorLangVersion>{VERSION}</RazorLangVersion>
</PropertyGroup>
Razor's language version is tightly integrated with the version of the runtime that it was built for. Targeting a language version that isn't designed for the runtime is unsupported and likely produces build errors.
Additional resources
- Standardizes the experience around building, packaging, and publishing projects containing Razor files for ASP.NET Core MVC-based projects.
- Includes a set of predefined targets, properties, and items that allow customizing the compilation of Razor files.
The Razor SDK includes a Content item with an Include attribute set to the **\*.cshtml globbing pattern. Matching files are published.
Prerequisites
Use the Razor SDK
Most web apps aren't required to explicitly reference the Razor SDK.
To use the Razor SDK to build class libraries containing Razor views or Razor Pages:
- Use - Microsoft.NET.Sdk.Razorinstead of- Microsoft.NET.Sdk:- <Project SDK="Microsoft.NET.Sdk.Razor"> <!-- omitted for brevity --> </Project>
- Typically, a package reference to - Microsoft.AspNetCore.Mvcis required to receive additional dependencies that are required to build and compile Razor Pages and Razor views. At a minimum, your project should add package references to:- Microsoft.AspNetCore.Razor.Design
- Microsoft.AspNetCore.Mvc.Razor.Extensions
- Microsoft.AspNetCore.Mvc.Razor
 - The - Microsoft.AspNetCore.Razor.Designpackage provides the Razor compilation tasks and targets for the project.- The preceding packages are included in - Microsoft.AspNetCore.Mvc. The following markup shows a project file that uses the Razor SDK to build Razor files for an ASP.NET Core Razor Pages app:- <Project Sdk="Microsoft.NET.Sdk.Razor"> <PropertyGroup> <TargetFramework>netcoreapp2.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" /> </ItemGroup> </Project>
Warning
The Microsoft.AspNetCore.Razor.Design and Microsoft.AspNetCore.Mvc.Razor.Extensions packages are included in the Microsoft.AspNetCore.App metapackage. However, the version-less Microsoft.AspNetCore.App package reference provides a metapackage to the app that doesn't include the latest version of Microsoft.AspNetCore.Razor.Design. Projects must reference a consistent version of Microsoft.AspNetCore.Razor.Design (or Microsoft.AspNetCore.Mvc) so that the latest build-time fixes for Razor are included. For more information, see this GitHub issue.
Properties
The following properties control the Razor's SDK behavior as part of a project build:
- RazorCompileOnBuild: When- true, compiles and emits the Razor assembly as part of building the project. Defaults to- true.
- RazorCompileOnPublish: When- true, compiles and emits the Razor assembly as part of publishing the project. Defaults to- true.
The properties and items in the following table are used to configure inputs and output to the Razor SDK.
| Items | Description | 
|---|---|
| RazorGenerate | Item elements ( .cshtmlfiles) that are inputs to code generation. | 
| RazorComponent | Item elements ( .razorfiles) that are inputs to Razor component code generation. | 
| RazorCompile | Item elements ( .csfiles) that are inputs to Razor compilation targets. Use thisItemGroupto specify additional files to be compiled into the Razor assembly. | 
| RazorTargetAssemblyAttribute | Item elements used to code generate attributes for the Razor assembly. For example: RazorAssemblyAttributeInclude="System.Reflection.AssemblyMetadataAttribute"_Parameter1="BuildSource" _Parameter2="https://free.blessedness.top/"> | 
| RazorEmbeddedResource | Item elements added as embedded resources to the generated Razor assembly. | 
| Property | Description | 
|---|---|
| RazorTargetName | File name (without extension) of the assembly produced by Razor. | 
| RazorOutputPath | The Razor output directory. | 
| RazorCompileToolset | Used to determine the toolset used to build the Razor assembly. Valid values are Implicit,RazorSDK, andPrecompilationTool. | 
| EnableDefaultContentItems | Default is true. Whentrue, includes web.config,.json, and.cshtmlfiles as content in the project. When referenced viaMicrosoft.NET.Sdk.Web, files under wwwroot and config files are also included. | 
| EnableDefaultRazorGenerateItems | When true, includes.cshtmlfiles fromContentitems inRazorGenerateitems. | 
| GenerateRazorTargetAssemblyInfo | When true, generates a.csfile containing attributes specified byRazorAssemblyAttributeand includes the file in the compile output. | 
| EnableDefaultRazorTargetAssemblyInfoAttributes | When true, adds a default set of assembly attributes toRazorAssemblyAttribute. | 
| CopyRazorGenerateFilesToPublishDirectory | When true, copiesRazorGenerateitems (.cshtml) files to the publish directory. Typically, Razor files aren't required for a published app if they participate in compilation at build-time or publish-time. Defaults tofalse. | 
| CopyRefAssembliesToPublishDirectory | When true, copy reference assembly items to the publish directory. Typically, reference assemblies aren't required for a published app if Razor compilation occurs at build-time or publish-time. Set totrueif your published app requires runtime compilation. For example, set the value totrueif the app modifies.cshtmlfiles at runtime or uses embedded views. Defaults tofalse. | 
| IncludeRazorContentInPack | When true, all Razor content items (.cshtmlfiles) are marked for inclusion in the generated NuGet package. Defaults tofalse. | 
| EmbedRazorGenerateSources | When true, adds RazorGenerate (.cshtml) items as embedded files to the generated Razor assembly. Defaults tofalse. | 
| UseRazorBuildServer | When true, uses a persistent build server process to offload code generation work. Defaults to the value ofUseSharedCompilation. | 
| GenerateMvcApplicationPartsAssemblyAttributes | When true, the SDK generates additional attributes used by MVC at runtime to perform application part discovery. | 
| DefaultWebContentItemExcludes | A globbing pattern for item elements that are to be excluded from the Contentitem group in projects targeting the Web or Razor SDK | 
| ExcludeConfigFilesFromBuildOutput | When true, .config and.jsonfiles do not get copied to the build output directory. | 
| AddRazorSupportForMvc | When true, configures the Razor SDK to add support for the MVC configuration that is required when building applications containing MVC views or Razor Pages. This property is implicitly set for .NET Core 3.0 or later projects targeting the Web SDK | 
| RazorLangVersion | The version of the Razor Language to target. | 
For more information on properties, see MSBuild properties.
Targets
The Razor SDK defines two primary targets:
- RazorGenerate: Code generates- .csfiles from- RazorGenerateitem elements. Use the- RazorGenerateDependsOnproperty to specify additional targets that can run before or after this target.
- RazorCompile: Compiles generated- .csfiles in to a Razor assembly. Use the- RazorCompileDependsOnto specify additional targets that can run before or after this target.
- RazorComponentGenerate: Code generates- .csfiles for- RazorComponentitem elements. Use the- RazorComponentGenerateDependsOnproperty to specify additional targets that can run before or after this target.
Runtime compilation of Razor views
- By default, the Razor SDK doesn't publish reference assemblies that are required to perform runtime compilation. This results in compilation failures when the application model relies on runtime compilation—for example, the app uses embedded views or changes views after the app is published. Set - CopyRefAssembliesToPublishDirectoryto- trueto continue publishing reference assemblies.
- For a web app, ensure your app is targeting the - Microsoft.NET.Sdk.WebSDK.
Razor language version
When targeting the Microsoft.NET.Sdk.Web SDK, the Razor language version is inferred from the app's target framework version. For projects targeting the Microsoft.NET.Sdk.Razor SDK or in the rare case that the app requires a different Razor language version than the inferred value, a version can be configured by setting the <RazorLangVersion> property in the app's project file:
<PropertyGroup>
  <RazorLangVersion>{VERSION}</RazorLangVersion>
</PropertyGroup>
Razor's language version is tightly integrated with the version of the runtime that it was built for. Targeting a language version that isn't designed for the runtime is unsupported and likely produces build errors.
Additional resources
ASP.NET Core