Skip to content

Instantly share code, notes, and snippets.

@pmachapman
Last active August 29, 2015 14:18
Show Gist options
  • Save pmachapman/c7a0303aca37d20fc90c to your computer and use it in GitHub Desktop.
Save pmachapman/c7a0303aca37d20fc90c to your computer and use it in GitHub Desktop.
Disables the VBHTML and WebForm view engines for ASP.NET MVC
// -----------------------------------------------------------------------
// <copyright file="DisableVbHtml.cs" company="Conglomo">
// Copyright 2015 Peter Chapman. Code in Public Domain.
// </copyright>
// -----------------------------------------------------------------------
namespace Conglomo.Website
{
using System;
using System.Linq;
using System.Web.Mvc;
/// <summary>
/// Extension methods for disabling VBHTML.
/// </summary>
public static class DisableVbHtml
{
/// <summary>
/// Returns a value indicating whether the specified <see cref="System.String"/> object occurs within this string.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="value">The string to seek.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the search.</param>
/// <returns>
/// <c>true</c> if the value parameter occurs within this string, or if value is the empty string (""); otherwise, <c>false</c>.
/// </returns>
public static bool Contains(this string source, string value, StringComparison comparisonType)
{
if (source != default(string) && value != default(string))
{
return source.IndexOf(value, comparisonType) >= 0;
}
else
{
return false;
}
}
/// <summary>
/// Disables the VBHTML views.
/// </summary>
/// <param name="engine">The engine.</param>
/// <returns>
/// A razor view engine without VBHTML support.
/// </returns>
/// <remarks>
/// Usage:
/// <c>
/// protected void Application_Start()
/// {
/// ViewEngines.Engines.Clear();
/// ViewEngines.Engines.Add(new RazorViewEngine().DisableVbhtml());
/// }
/// </c>
/// </remarks>
public static RazorViewEngine DisableVbhtml(this RazorViewEngine engine)
{
if (engine != default(RazorViewEngine))
{
engine.AreaViewLocationFormats = DisableVbHtml.FilterOutVbhtml(engine.AreaViewLocationFormats);
engine.AreaMasterLocationFormats = DisableVbHtml.FilterOutVbhtml(engine.AreaMasterLocationFormats);
engine.AreaPartialViewLocationFormats = DisableVbHtml.FilterOutVbhtml(engine.AreaPartialViewLocationFormats);
engine.ViewLocationFormats = DisableVbHtml.FilterOutVbhtml(engine.ViewLocationFormats);
engine.MasterLocationFormats = DisableVbHtml.FilterOutVbhtml(engine.MasterLocationFormats);
engine.PartialViewLocationFormats = DisableVbHtml.FilterOutVbhtml(engine.PartialViewLocationFormats);
engine.FileExtensions = DisableVbHtml.FilterOutVbhtml(engine.FileExtensions);
}
return engine;
}
/// <summary>
/// Filters the out VBHTML views.
/// </summary>
/// <param name="source">The source.</param>
/// <returns>
/// The views with only CSHTML.
/// </returns>
private static string[] FilterOutVbhtml(string[] source)
{
if (source != default(string[]))
{
return source.Where(s => !s.Contains("vbhtml", StringComparison.OrdinalIgnoreCase)).ToArray();
}
else
{
return source;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment