首页 > 解决方案 > PrecompiledMvc​​Engine 仍然使用共享物理视图

问题描述

我已经在 app start 中注册了一个 PrecompilecMvcEngine 来预编译我的剃刀视图。构建包正确地排除了 ~/Views/ 目录中的所有 .cshtml 文件,但是如果目录中存在视图,它会继续使用 .cshtml 文件,无论它是否编译。

例如,视图 ~/Shared/Layout.cshtml 如果存在于部署目录中,则始终使用。

如何确保从不使用物理视图?

我有以下代码:

[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(SomeBaseNamespace.Views.RazorGeneratorMvcStart), "Start")]

namespace SomeBaseNamespace.Views
{
    public static class RazorGeneratorMvcStart
    {
        public static void Start()
        {
            var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) 
            {
                UsePhysicalViewsIfNewer = false // I would expect this to prevent the engine from using physical views.
            };

            ViewEngines.Engines.Insert(0, engine);
        }
    }
}

标签: c#asp.net-mvcrazorrazorengine

解决方案


正如CodeCaster所写,设置PreemptPhysicalFilestrueonPrecompiledMvcEngine解决了这个问题。


推荐阅读