Skip to content

Instantly share code, notes, and snippets.

@mhornbacher
Last active March 8, 2021 20:17
Show Gist options
  • Save mhornbacher/5fa5a3bb4c4419353170fb12304d9366 to your computer and use it in GitHub Desktop.
Save mhornbacher/5fa5a3bb4c4419353170fb12304d9366 to your computer and use it in GitHub Desktop.
WebForms and Razor View Engine Example
public class BaseViewEngine : RazorViewEngine
{
protected BaseViewEngine()
{
this.AreaViewLocationFormats = new string[4]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/{1}/{0}.aspx",
"~/Areas/{2}/Views/{1}/{0}.ascx",
"~/Areas/{2}/Views/Shared/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.aspx",
"~/Areas/{2}/Views/Shared/{0}.ascx"
};
this.AreaMasterLocationFormats = new string[4]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/{1}/{0}.master",
"~/Areas/{2}/Views/Shared/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.master"
};
this.ViewLocationFormats = new string[4]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.aspx",
"~/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.aspx",
"~/Views/Shared/{0}.ascx"
};
this.MasterLocationFormats = new string[4]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.master",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.master"
};
this.PartialViewLocationFormats = this.ViewLocationFormats;
this.AreaPartialViewLocationFormats = this.AreaViewLocationFormats;
FileExtensions = new[] { "cshtml", "aspx", "ascx", "master" };
}
protected override IView PartialView(ControllerContext context, string path)
=> IsRazor(path) ? base.CreatePartialView(context, path) : WebFormsView(context, path);
protected override IView View(ControllerContext context, string path, string masterPath)
=> IsRazor(path) ? base.CreateView(context, path, masterPath) : WebFormsView(context, path, masterPath);
private IView WebFormsView(ControllerContext context, string path, string masterPath = null)
=> new WebFormView(context, path, masterPath, ViewPageActivator);
private static bool IsRazor(string path) => path.EndsWith(".cshtml");
}
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<!-- add any namespaces you need included here -->
</namespaces>
</pages>
</system.web.webPages.razor>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment