MVC6 是否支持预编译视图? [英] Does MVC6 support Precompiled Views?

查看:13
本文介绍了MVC6 是否支持预编译视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<MVC6 我可以在发布时预编译 .cshtml 文件,这样它们就不必在请求时在第一次点击时进行编译.

是否可以在 MVC6 中预编译 .cshtml 文件?

解决方案

Visual Studio 2017 的答案:

从 Visual Studio 解决方案资源管理器中编辑您的项目 .csproj,并添加 MvcRazorCompileOnPublishPreserveCompilationContext 属性,如果它不存在,则值为 true

<PropertyGroup><TargetFramework>netcoreapp1.1</TargetFramework>……<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish><PreserveCompilationContext>true</PreserveCompilationContext></属性组>

通过 nuget 或编辑 .csproj 将包 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation 添加到您的项目中

...<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" 版本="1.1.0"/></项目组>

<小时>

**以下答案仅适用于 ASP.NET Core RC1.**

您可以创建一个继承自 RazorPreCompileModule 的类并重写 EnablePreCompilation 方法以将 razor 预编译设置为 true.

使用 Microsoft.AspNet.Mvc.Razor.Precompilation;使用 Microsoft.Dnx.Compilation.CSharp;命名空间预编译网站{公共类 RazorPreCompilation:RazorPreCompileModule{protected override bool EnablePreCompilation(BeforeCompileContext context) =>真的;}}

Startup.cs中引用这个方法:

公共类启动{//设置应用服务公共无效配置服务(IServiceCollection 服务){//将 MVC 服务添加到服务容器服务.AddMvc().AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly);}公共无效配置(IApplicationBuilder 应用程序){app.UseCultureReplacer();app.UseMvcWithDefaultRoute();}}

你可以看一下预编译示例

这会将整个应用程序编译为 nuget 包.

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?

解决方案

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.

这篇关于MVC6 是否支持预编译视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆