Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ASP.NET MVC 3 Razor View Engine ignore .vbhtml files

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.

like image 483
bkaid Avatar asked May 18 '11 18:05

bkaid


4 Answers

This is the code for your custom view engine:

public class CSRazorViewEngine : RazorViewEngine {

    public CSRazorViewEngine() {

        base.AreaViewLocationFormats = new string[] { 
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml"
        };

        base.AreaMasterLocationFormats = new string[] { 
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml"
        };

        base.AreaPartialViewLocationFormats = new string[] { 
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml"
        };

        base.ViewLocationFormats = new string[] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };

        base.PartialViewLocationFormats = new string[] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };

        base.MasterLocationFormats = new string[] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };

        base.FileExtensions = new string[] { "cshtml" };
    }
}

Register this on Application_Start method like this:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CSRazorViewEngine());
like image 169
tugberk Avatar answered Nov 16 '22 03:11

tugberk


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();
}
like image 32
Dima Stefantsov Avatar answered Nov 16 '22 01:11

Dima Stefantsov


There is no point in doing this.

However, if you really want to, you can clear ViewEngines, then register your own RazorViewEngine with FileExtensions set to ".cshtml".

like image 42
SLaks Avatar answered Nov 16 '22 01:11

SLaks


I figured I'd merge the examples from @tugberk and @Dimps:

public class CSHtmlRazorViewEngine : RazorViewEngine {
    public CSHtmlRazorViewEngine()
        : base() {
        this.AreaMasterLocationFormats = Filter(base.AreaMasterLocationFormats);
        this.AreaPartialViewLocationFormats = Filter(base.AreaPartialViewLocationFormats);
        this.AreaViewLocationFormats = Filter(base.AreaViewLocationFormats);
        this.FileExtensions = Filter(base.FileExtensions);
        this.MasterLocationFormats = Filter(base.MasterLocationFormats);
        this.PartialViewLocationFormats = Filter(base.PartialViewLocationFormats);
        this.ViewLocationFormats = Filter(base.ViewLocationFormats);
    }

    private static string[] Filter(
        string[] source) {
        return source.Where(
            s =>
                s.Contains("cshtml")).ToArray();
    }
}
like image 33
Gup3rSuR4c Avatar answered Nov 16 '22 01:11

Gup3rSuR4c