In < MVC6 I could have .cshtml files pre-compiled on publish, so that they didn\'t have to be compiled on first hit when requested.
Is it possible to precompile .
Add a reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0-msbuild3-update1" PrivateAssets="All" />
</ItemGroup>
Turn on razor view compilation upon publishing the project.
<PropertyGroup>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>
See this GitHub issue for more details.
Razor Pre-compilation was dropped in ASP.NET Core 1.0 MVC 6 RC2 but was brought back in ASP.NET Core 1.1 using a tool which you can add like so:
Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design
under the dependencies
section like so:"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design": { "type": "build", "version": "1.1.0-preview4-final" }
Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools
under the tools
section like so:"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools": "1.1.0-preview4-final",
"scripts": { "postpublish": "dotnet razor-precompile --configuration %publish:Configuration% --framework %publish:TargetFramework% --output-path %publish:OutputPath% %publish:ProjectPath%" }
Answer for Visual Studio 2017:
Edit your project .csproj from Visual Studio Solution Explorer and add MvcRazorCompileOnPublish
and PreserveCompilationContext
properties with value of true if it does not already exists
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
....
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
Add the package Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
to your project via nuget or editing the .csproj
<ItemGroup>
...
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0" />
</ItemGroup>
**The following answer was only applicable to ASP.NET Core RC1. **
You can create a class that inherits from RazorPreCompileModule
and override the EnablePreCompilation
method to set razor precompilation as true.
using Microsoft.AspNet.Mvc.Razor.Precompilation;
using Microsoft.Dnx.Compilation.CSharp;
namespace PrecompilationWebSite
{
public class RazorPreCompilation : RazorPreCompileModule
{
protected override bool EnablePreCompilation(BeforeCompileContext context) => true;
}
}
In Startup.cs
reference this method:
public class Startup
{
// Set up application services
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container
services
.AddMvc()
.AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly);
}
public void Configure(IApplicationBuilder app)
{
app.UseCultureReplacer();
app.UseMvcWithDefaultRoute();
}
}
You can look at the precompilation example project on the asp.net github page for the whole project.
You could also alternatively compile your whole app when you publish it.
which will publish the whole app compiled as a nuget package.
Finally Razor view pre-compilation is supported as of ASP.NET Core 1.1.
Here is the announcement: Announcing the Fastest ASP.NET Yet, ASP.NET Core 1.1 RTM
It said:
The Razor syntax for views provides a flexible development experience where compilation of the views happens automatically at runtime when the view is executed. However, there are some scenarios where you do not want the Razor syntax compiled at runtime. You can now compile the Razor views that your application references and deploy them with your application. To enable view compilation as part of publishing your application,
- Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design” under the “dependencies” section.
- Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools” under the tools section
- Add a postpublish script to invoke view compiler:
"scripts": {
"postpublish": "dotnet razor-precompile --configuration %publish:Configuration% --framework %publish:TargetFramework% --output-path %publish:OutputPath% %publish:ProjectPath%"
}