Asp.net core 如何使用ASP.NET内核获取视图中的编译时错误
在Mvc6中,如何在视图和视图组件中获得编译时错误 在MVC5中,编辑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; } }
.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
类:ConfigureServices
方法中添加对AddPrecompiledRazorViews
的调用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 ...
}