Search code examples
c#asp.net-mvcrazorrazorengine

PrecompiledMvcEngine still uses shared physical views


I have registered a PrecompilecMvcEngine in app start to precompile my razor views. The build package correctly excludes all .cshtml-files in the ~/Views/ directory, but if a view is present in the directory, it continues to use the .cshtml file, whether it compiles or not.

For example, the view ~/Shared/Layout.cshtml is always used if present in the deployment directory.

How can I ensure physical views are never used?

I have the following code:

[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);
        }
    }
}

Solution

  • As CodeCaster wrote, setting PreemptPhysicalFiles to true on PrecompiledMvcEngine solved the problem.