Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

asp.net core - Does MVC6 support Precompiled Views?

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 .cshtml files in MVC6?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

csproj (VS 2017) Answer

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.

xproj and project.json (VS 2015) Answer

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:

  1. Add a reference to 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"
}
  1. Add a reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools under the tools section like so:
"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools": "1.1.0-preview4-final",
  1. 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%"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...