加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 如何使用ASP.NET CORE在视图中获取编译时错误

发布时间:2020-12-16 03:24:10 所属栏目:asp.Net 来源:网络整理
导读:如何在视图中查看编译时错误并在Mvc6中查看组件? 在MVC 5中,编辑.csproj文件很简单.我们现在怎么做? 解决方法 RC-1的答案 创建以下RazorPreCompilation类: namespace YourApp.Compiler.Preprocess{ public sealed class RazorPreCompilation : RazorPreCo
如何在视图中查看编译时错误并在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中.

enter image description here

>在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 ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读