Search code examples
c#razormsbuildrazorgenerator

Base class for view with RazorGenerator precompiled views


I have console project with some .cshtml files stored in Resources folder, which is built into .cs files with Razorgenerator.MsBuild nuget package.

I need to replace WebViewPage view base class for generated view, but adding web.config file with pageBaseType="ConsoleApp.MyViewBase" option set or changing location of files in different ways did not help me and no any other ways where found.

View class is always generated as:

public partial class _Resources_Index_cshtml : 
         System.Web.Mvc.WebViewPage<ConsoleApp.ModelClass> { ... }

Hope, there is some way to confgure the class globally for all views, without adding directives at the beginning of every .cshtml file.


Solution

  • It looks that that setting base view in web.config works in latest RazorGenerator.

    So update your RazorGenerator package and try to have something like this in web.config:

    <configSections>    
      <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <pages pageBaseType="WebApplication2.Views.BaseView2">
        <namespaces>
          <add namespace="System.Web.Mvc" />
          <add namespace="System.Web.Mvc.Ajax" />
          <add namespace="System.Web.Mvc.Html" />
          <add namespace="System.Web.Optimization"/>
          <add namespace="System.Web.Routing" />        
        </namespaces>
      </pages>
    </system.web.webPages.razor>
    

    In my case I was adding precompilation (with RazorGenerator.Mvc and RazorGenerator.MsBuild packages) to old project and it didn't work. The problem was that I had Version=2.0.0.0 in my configSections project and had to change it to Version=3.0.0.0.