Monday, August 6, 2012

How To Solve Type 'ASP._Page__ViewStart_cshtml' does not inherit from 'System.Web.WebPages.StartPage'

OK. So you are using Asp.Net MVC 3 or 4 (or maybe even 2). You have created several Areas in your project and you have decided to move your _ViewStart.cshtml file to your root directory so you are adhering to the DRY principal and all of your Views in the main area of your site AND in your Areas will use it. You add the file to the root and you are really happy with yourself. There is just one problem. After you run your beautiful project, you get a nice big yellow screen complete with the following message -


Type 'ASP._Page__ViewStart_cshtml' does not inherit from 'System.Web.WebPages.StartPage'.

Great! Now what? The solution to this issue is simple. When you create an Area in an MVC application, the following information is automatically added to your web.config.

 
<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" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>  

 

However, when you move that _ViewStart.cshtml file to the main area of your site, your main web.config does NOT have those areas in there already. You have to add them! Once you add those items to your web.config, you will be good to go. Add those sections, rebuild your project and now your Area Views and your main Views will all use the _Viewstart.cshtml file.


Problem:
You moved your _ViewStart.cshtml file to the root of your site and got the following error:

Type 'ASP._Page__ViewStart_cshtml' does not inherit from 'System.Web.WebPages.StartPage'.

Solution:
Add the following to your web.config file and recompile your application.

  <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" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

 

That's it!

Smooches,

Kila Morton