VS2019 - 当前上下文中不存在 ViewBag [英] VS2019 - ViewBag does't exist in current context

查看:144
本文介绍了VS2019 - 当前上下文中不存在 ViewBag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 VS 2019 for Mac 并开始构建应用程序.在我看来,我收到以下错误:当前上下文中不存在名称 ViewBag.名称 Layout 在当前上下文中不存在.

I have installed VS 2019 for Mac and started building an application. In my view, I get the following errors: The name ViewBag does not exist in the current context. The name Layout does not exist in the current context.

问题的原因可能是什么,我们如何解决这个问题?

What could be the cause for the issue and How could we resolve this?

推荐答案

您可能缺少 ../Views/Web.config 文件,因为您是从空的 ASP.NET 创建项目应用程序而不是使用 ASP.NET MVC 模板.或者您需要将特定于 MVC 的 Razor 配置添加到您的 web.config.

You might be missing the ../Views/Web.config file, because you created the project from an empty ASP.NET application instead of using an ASP.NET MVC template. Or You need to add MVC-specific Razor configuration to your web.config.

在您的 web.config 文件中,您应该添加以下几行:

In your web.config file you should add the following lines:

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

这篇关于VS2019 - 当前上下文中不存在 ViewBag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆