MVC6 是否支持预编译视图?
Does MVC6 support Precompiled Views?
在
是否可以在 MVC6 中预编译 .cshtml 文件?
2017 年 Visual Studio 的答案:
从 Visual Studio 解决方案资源管理器编辑您的项目 .csproj 并添加 MvcRazorCompileOnPublish
和 PreserveCompilationContext
值为 true 的属性(如果尚不存在)
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
....
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
通过 nuget 或编辑 .csproj
将包 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
添加到您的项目
<ItemGroup>
...
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0" />
</ItemGroup>
**以下答案仅适用于 ASP.NET Core RC1。 **
您可以创建一个继承自 RazorPreCompileModule
的 class 并覆盖 EnablePreCompilation
方法以将 razor 预编译设置为 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;
}
}
在Startup.cs
中参考这个方法:
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();
}
}
你可以在asp.net github页面查看整个项目的预编译示例project。
您也可以在发布时编译整个应用程序。
这将发布编译为 nuget 包的整个应用程序。
csproj(VS 2017)答案
添加对 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
的引用:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0-msbuild3-update1" PrivateAssets="All" />
</ItemGroup>
发布项目时打开 razor 视图编译。
<PropertyGroup>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>
有关详细信息,请参阅 this GitHub 问题。
xproj 和 project.json (VS 2015) 答案
Razor 预编译已在 ASP.NET Core 1.0 MVC 6 RC2 中删除,但在 ASP.NET Core 1.1 中 brought back 使用可以添加的工具,如下所示:
- 在
dependencies
部分下添加对 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design
的引用,如下所示:
"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design": {
"type": "build",
"version": "1.1.0-preview4-final"
}
- 在
tools
部分下添加对 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools
的引用,如下所示:
"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%"
}
最终从 ASP.NET Core 1.1 开始支持 Razor 视图预编译。
公告如下:Announcing the Fastest ASP.NET Yet, ASP.NET Core 1.1 RTM
它说:
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%"
}
在 是否可以在 MVC6 中预编译 .cshtml 文件?
2017 年 Visual Studio 的答案:
从 Visual Studio 解决方案资源管理器编辑您的项目 .csproj 并添加 MvcRazorCompileOnPublish
和 PreserveCompilationContext
值为 true 的属性(如果尚不存在)
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
....
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
通过 nuget 或编辑 .csproj
将包Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
添加到您的项目
<ItemGroup>
...
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0" />
</ItemGroup>
**以下答案仅适用于 ASP.NET Core RC1。 **
您可以创建一个继承自 RazorPreCompileModule
的 class 并覆盖 EnablePreCompilation
方法以将 razor 预编译设置为 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;
}
}
在Startup.cs
中参考这个方法:
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();
}
}
你可以在asp.net github页面查看整个项目的预编译示例project。
您也可以在发布时编译整个应用程序。
这将发布编译为 nuget 包的整个应用程序。
csproj(VS 2017)答案
添加对 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
的引用:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0-msbuild3-update1" PrivateAssets="All" />
</ItemGroup>
发布项目时打开 razor 视图编译。
<PropertyGroup>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>
有关详细信息,请参阅 this GitHub 问题。
xproj 和 project.json (VS 2015) 答案
Razor 预编译已在 ASP.NET Core 1.0 MVC 6 RC2 中删除,但在 ASP.NET Core 1.1 中 brought back 使用可以添加的工具,如下所示:
- 在
dependencies
部分下添加对Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design
的引用,如下所示:
"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design": { "type": "build", "version": "1.1.0-preview4-final" }
- 在
tools
部分下添加对Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools
的引用,如下所示:
"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%" }
最终从 ASP.NET Core 1.1 开始支持 Razor 视图预编译。
公告如下:Announcing the Fastest ASP.NET Yet, ASP.NET Core 1.1 RTM
它说:
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%"
}