Make ASP.NET MVC 3 Razor View Engine ignore .vbhtm

2019-02-01 01:52发布

How can I remove the VB Razor Engine or configure the RazorViewEngine to not use and look for .vbhtml files on disk? For new ASP.NET MVC 3 Razor projects, I always remove the WebFormViewEngine in Application_Start because I won't ever be using it and so I don't need it to search for .aspx, .ascx or .master files on disk. For this same reason I would like to avoid .vbhtml file searching.

7条回答
闹够了就滚
2楼-- · 2019-02-01 02:41

Why is there no point in removing search in .vbhtml if I'm never going to use it?

Here is how I do it (but I still don't know is it a good thing to remove vbhtml):

protected void Application_Start()
{
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new RazorViewEngine().DisableVbhtml());

    ...
}

Extension method code:

public static RazorViewEngine DisableVbhtml(this RazorViewEngine engine)
{
    engine.AreaViewLocationFormats = FilterOutVbhtml(engine.AreaViewLocationFormats);
    engine.AreaMasterLocationFormats = FilterOutVbhtml(engine.AreaMasterLocationFormats);
    engine.AreaPartialViewLocationFormats = FilterOutVbhtml(engine.AreaPartialViewLocationFormats);
    engine.ViewLocationFormats = FilterOutVbhtml(engine.ViewLocationFormats);
    engine.MasterLocationFormats = FilterOutVbhtml(engine.MasterLocationFormats);
    engine.PartialViewLocationFormats = FilterOutVbhtml(engine.PartialViewLocationFormats);
    engine.FileExtensions = FilterOutVbhtml(engine.FileExtensions);

    return engine;
}

private static string[] FilterOutVbhtml(string[] source)
{
    return source.Where(s => !s.Contains("vbhtml")).ToArray();
}
查看更多
登录 后发表回答