如何在视图中查看编译时错误并在Mvc6中查看组件?
在MVC 5中,编辑.csproj文件很简单.我们现在怎么做?
解决方法
RC-1的答案
>创建以下RazorPreCompilation类:
namespace YourApp.Compiler.Preprocess { public sealed class RazorPreCompilation : RazorPreCompileModule { protected override bool EnablePreCompilation(BeforeCompileContext context) => true; } }
>将此类放在{root} / Compiler / Preprocess中.
>在ConfigureServices方法中添加对AddPrecompiledRazorViews的调用.
public void ConfigureServices(IServiceCollection services) { // ... other code ... services .AddMvc() .AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly); // ... other code ... }
正如其他答案中所述,当预编译视图时,您无法在应用程序运行时更改它们.此外,有时您必须在项目上进行重建(而不仅仅是构建),以便新代码更改生效.例如,在重建项目之前,纠正Razor方法调用中的拼写错误仍可能会触发编译错误.
指令更新
如果您希望使用预处理指令运行,请将它们包装在AddPrecompiledRazorViews调用中,如下所示:
public void ConfigureServices(IServiceCollection services) { // ... other code ... var mvcBuilder = services.AddMvc() #if !DEBUG mvcBuilder.AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly); #endif // ... other code ... }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。