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.
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());
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();
}
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"
.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With