How do I use a common _ViewStart in areas?

2019-01-16 09:16发布

问题:

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'.

I dunno what I'm doing wrong?

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

How can I use _ViewStart.vbhtml in Areas?

回答1:

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>


回答2:

When I ran across this problem, I ran across this answer first but what I really wanted was on http://stevescodingblog.co.uk/asp-net-mvc-3rc-areas-viewstart/.

The gist of the issue is that _ViewStart.**html has a scope. It will apply to any views that are on the same level or in subfolders under it. Therefore, if you move it to the base directory (e.g. next to the Global.asax file), it will apply for all views under ~/Views/* and all views under ~/Areas/*/Views/*.

Similar to the accepted answer, you'll still have to copy the <system.web.webPages.razor> and <sectionGroup name="system.web.webPages.razor"..> sections. Place them in your base web.config file (in the root of the project).

Here's a more complete tutorial.

For bonus points, you can override the _ViewStart.**html settings by creating a new file closer to the view in question (e.g. the file ~/Views/_ViewStart.cshtml will overwrite ~/_ViewStart.cshtml for all views in the ~/Views/ directory).



回答3:

I found that moving _ViewStart to the root and adding the system.web.webPages.razor section to the root web config worked, however I also needed to add a number of lines similar to this << add namespace="Ico.Logics.Web.Areas.Admin.Models" />> to the copied section.