Search code examples
asp.net-mvcvb.netrazor

How do I use a common _ViewStart in areas?


In my "root" Views folder, I have a _ViewStart with the following code:

@Code
    Layout = "~/Views/Shared/_Layout.vbhtml"
End COde

In my Area/Public/Views folder, I have a copy of my _ViewStart from the root Views folder.

But when I run the code, I get this error:

Unable to cast object of type 'ASP._ViewStart_vbhtml' to type 'System.Web.WebPages.StartPage'.

What I'm doing wrong?

Can I use one _ViewStart.vbhtml for my areas too?

How can I use _ViewStart.vbhtml in Areas?


Solution

  • You need to copy the ~\Views\Web.config file (or at least the following configuration elements) into your Area's View Web.Config:

    <configSections>
      <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.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>