Does MVC6 support Precompiled Views?

后端 未结 3 1361
礼貌的吻别
礼貌的吻别 2020-12-09 18:25

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 .

相关标签:
3条回答
  • 2020-12-09 18:35

    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%"
    }
    
    0 讨论(0)
  • 2020-12-09 18:37

    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.

    0 讨论(0)
  • 2020-12-09 18:55

    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,

    1. Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design” under the “dependencies” section.
    2. Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools” under the tools section
    3. 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%"
    }
    
    0 讨论(0)
提交回复
热议问题