Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 如何使用ASP.NET内核获取视图中的编译时错误_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

Asp.net core 如何使用ASP.NET内核获取视图中的编译时错误

Asp.net core 如何使用ASP.NET内核获取视图中的编译时错误,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,在Mvc6中,如何在视图和视图组件中获得编译时错误 在MVC5中,编辑.csproj文件很简单。我们现在怎么做?您必须添加一个类 public class RazorPreCompilation : RazorPreCompileModule { public RazorPreCompilation(IServiceProvider provider) : base(provider) { this.GenerateSymbols = true; } }

在Mvc6中,如何在视图和视图组件中获得编译时错误

在MVC5中,编辑
.csproj
文件很简单。我们现在怎么做?

您必须添加一个类

public class RazorPreCompilation : RazorPreCompileModule
{
    public RazorPreCompilation(IServiceProvider provider) : base(provider)
    {
        this.GenerateSymbols = true;
    }
}
并将
AddPrecompiledRazorViews
添加到startup类中的mvc设置中


参见此

您可以通过向项目中添加以下类来使用预编译视图。但是,这样做会使您无法在运行时编辑视图。因此,我添加了
#if!DEBUG
预处理器指令,以便仅在发布模式下预编译视图

#if !DEBUG
namespace MvcBoilerplate.Compiler.Preprocess
{
    using System;
    using Microsoft.AspNet.Mvc;

    /// <summary>
    /// Enable pre-compilation of Razor views, so that errors in your .cshtml files are caught and displayed 
    /// in the Visual Studio errors window at compile time, rather than your sites users receiving a runtime 500 
    /// internal server error. Pre-compilation may reduce the time it takes to build and launch your project but will 
    /// cause the build time to increase. It will also stop edit and continue working for .cshtml files.
    /// </summary>
    public class RazorPreCompilation : RazorPreCompileModule
    {
    }
}
#endif
预编译视图提供了更快的应用程序启动性能。

回答RC-1
  • 创建以下
    RazorPreCompilation
    类:
  • 将此类放置在{root}/Compiler/Preprocess中
  • ConfigureServices
    方法中添加对
    AddPrecompiledRazorViews
    的调用
  • 正如在其他答案中所指出的,当视图被预编译时,您将失去在应用程序运行时更改视图的能力。此外,有时为了使新的代码更改生效,您似乎必须对项目进行重新构建(而不仅仅是构建)。例如,在重新生成项目之前,更正Razor方法调用中的键入错误仍可能触发编译错误

    指令更新 如果希望使用预处理指令运行此命令,请将它们围绕
    AddPrecompiledRazorViews
    调用,如下所示:

    public void ConfigureServices(IServiceCollection services)
    {
        // ... other code ...
        var mvcBuilder = services.AddMvc()
        
    #if !DEBUG
        mvcBuilder.AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly);
    #endif
    
        // ... other code ...
    }
    

    在Beta 8中,情况发生了变化。看看我的答案。这在beta 8中似乎不起作用?我在视图中有错误,但在构建时未被拾取。
    namespace YourApp.Compiler.Preprocess
    {
        public sealed class RazorPreCompilation : RazorPreCompileModule
        {
            protected override bool EnablePreCompilation(BeforeCompileContext context) => true;
        }
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        // ... other code ...
        services
            .AddMvc()
            .AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly);
        // ... other code ...
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        // ... other code ...
        var mvcBuilder = services.AddMvc()
        
    #if !DEBUG
        mvcBuilder.AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly);
    #endif
    
        // ... other code ...
    }