asp.net-mvc 如何在区域中使用常见的 _ViewStart?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4109205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:33:40  来源:igfitidea点击:

How do I use a common _ViewStart in areas?

asp.net-mvcvb.netrazor

提问by MojoDK

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

在我的“根”视图文件夹中,我有一个带有以下代码的 _ViewStart:

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

在我的 Area/Public/Views 文件夹中,我有一个来自根视图文件夹的 _ViewStart 的副本。

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.vbhtmlfor my areas too?

我也可以_ViewStart.vbhtml在我的区域使用一个吗?

How can I use _ViewStart.vbhtmlin Areas?

如何_ViewStart.vbhtml在区域中使用?

采纳答案by marcind

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

您需要将~\Views\Web.config文件(或至少以下配置元素)复制到您的区域的视图 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>

回答by frank hadder

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

当我遇到这个问题时,我首先遇到了这个答案,但我真正想要的是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/* andall views under ~/Areas/*/Views/*.

问题的要点是 _ViewStart.**html 有一个范围。它将应用于处于同一级别或其下的子文件夹中的任何视图。因此,如果您将它移动到基本目录(例如在 Global.asax 文件旁边),它将适用于 ~/Views/*下的所有视图~/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).

与接受的答案类似,您仍然需要复制<system.web.webPages.razor><sectionGroup name="system.web.webPages.razor"..>部分。将它们放在您的基本 web.config 文件中(在项目的根目录中)。

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

对于奖励积分,您可以通过创建一个更接近相关视图的新文件来覆盖 _ViewStart.**html 设置(例如,文件 ~/Views/_ViewStart.cshtml 将覆盖 ~/_ViewStart.cshtml 中所有视图的 ~/视图/目录)。

回答by Robert Taylor

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.

我发现将 _ViewStart 移动到根目录并将 system.web.webPages.razor 部分添加到根 web 配置中是有效的,但是我还需要添加一些类似于这个 << add namespace="Ico.Logics.Web 的行.Areas.Admin.Models"/>> 到复制的部分。