【问题标题】:Layout does not exist in the current context当前上下文中不存在布局
【发布时间】:2020-04-16 11:02:47
【问题描述】:

我正在尝试将布局包含在我的一个视图中,它说它与 viewbag 一起在当前上下文中不存在。

@{
ViewBag.Title = "About Me";
Layout = "~/Views/Shared/_Layout.cshtml";}

我查看了其他解决方案,说我在 web.config 中需要这个或那个,但没有任何效果。

我在 Mac OS 上使用 Visual Studio for Mac 社区

views/web.config:

<configuration>
 <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.7.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" />
             <add namespace="GroundRoots" />
         </namespaces>
     </pages>
 </system.web.webPages.razor>
 <appSettings>
     <add key="webpages:Enabled" value="false" />
 </appSettings>
 <system.webServer>
     <handlers>
         <remove name="BlockViewHandler" />
         <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
     </handlers>
 </system.webServer>
</configuration>

根 web.config:

<!--
Web.config file for GroundRoots.

The settings that can be used in this file are documented at 
http://www.mono-project.com/Config_system.web and 
http://msdn2.microsoft.com/en-us/library/b5ysx397.aspx
-->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies />
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

<system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer></configuration>

不确定从这里去哪里......

【问题讨论】:

    标签: asp.net macos visual-studio model-view-controller


    【解决方案1】:

    布局是否适用于其他视图?尝试创建一个新视图并选中“使用布局页面”,然后选择您的布局视图文件。希望这行得通,它一直对我有用

    【讨论】:

    • 它只适用于主页。当我创建一个新视图并使用布局页面时,它会创建包含“view.bag”和“layout”位的文件。然而,它们不起作用,因为它说它们在当前上下文中不存在。
    • 你的视图有控制器吗?即使它是空白的,您也总是需要向视图中添加一个动作。例如,您在控制器上有索引和索引视图..
    • 我愿意。我有一个 homeController 用于索引和我的其他视图。基本上我遇到的真正问题是我已经制作了一个页眉和页脚样式,我想在所有视图中保留它,但只有主索引视图维护所有 css。因此,当我尝试从我的 _layout 继承另一个视图时,没有任何样式出现。我想这是因为它没有识别布局调用?
    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 2023-03-30
    • 2019-08-05
    • 2020-07-31
    • 2021-01-16
    • 1970-01-01
    • 2018-06-07
    相关资源
    最近更新 更多