ASP.NET Core Razor SDK
The .NET Core 2.1 SDK or later includes the
Microsoft.NET.Sdk.Razor MSBuild SDK (Razor SDK). The Razor SDK:
- 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.
Using the Razor SDK
Most web apps don't need to expressly reference the Razor SDK.
To use the Razor SDK to build class libraries containing Razor views or Razor Pages:
<Project SDK="Microsoft.NET.Sdk.Razor"> ... </Project>
Typically a package reference to
Microsoft.AspNetCore.Mvcis required to bring in additional dependencies required to build and compile Razor Pages and Razor views. At minimum, your project needs to add package references to:
The preceding packages are included in
Microsoft.AspNetCore.Mvc. The following markup shows a basic .csproj 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.0" /> </ItemGroup> </Project>
The following properties control the Razor's SDK behavior as part of a project build:
true, compiles and emits the Razor assembly as part of building the project. Defaults to
true, compiles and emits the Razor assembly as part of publishing the project. Defaults to
The following properties and items are used to configure inputs and output to the Razor SDK:
|RazorGenerate||Item elements (.cshtml files) that are inputs to code generation targets.|
|RazorCompile||Item elements (.cs files) that are inputs to Razor compilation targets. Use this ItemGroup to specify additional files to be compiled into the Razor assembly.|
|RazorTargetAssemblyAttribute||Item elements used to code generate attributes for the Razor assembly. For example:
|RazorEmbeddedResource||Item elements added as embedded resources to the generated Razor assembly|
|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
The Razor SDK defines two primary targets:
RazorGenerate- Code generates .cs files from RazorGenerate item elements. Use
RazorGenerateDependsOnproperty to specify additional targets that can run before or after this target.
RazorCompile- Compiles generated .cs files in to a Razor assembly. Use
RazorCompileDependsOnto 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
trueto continue publishing reference assemblies.
For web applications, ensure your app is targeting