Visual Studio无法解析模型,但会加载Razor页面

班克斯·桑

我有一个MVC 5应用程序,由空模板制成。我已经Microsoft.AspNet.Mvc通过Nuget添加了,我的包文件看起来像这样:

<packages>
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net46" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net46" developmentDependency="true" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" />
</packages>

我有一个Razor页面,该页面声明一个模型@model IEnumerable<Models.ToDoModel>并在foreach循环中使用它。

@model IEnumerable<Models.ToDoModel>

<!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Home</title>
    </head>
    <body>
    <h1>Hello World!</h1>
    <ul>
        @foreach (var item in Model)
        {
            <li>ID: @item.Id</li>
        }
    </ul>
    </body>
</html>

当我运行该页面时,它呈现得很好,但是Visual Studio始终表示存在错误:

无法解析符号“模型”

它不会识别该model关键字,也不会给我任何Intellisense。

Web.Config看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.6"/>
    <httpRuntime targetFramework="4.6"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
  <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.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Web.ConfigViews文件夹的样子:

<?xml version="1.0"?>

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

  <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.configs都很好。Visual Studio经常会对这种事情感到困惑。我的建议是关闭Visual Studio并重新打开它。另外,正如您所发现的,可能需要关闭VS的所有打开的实例。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Visual Studio 2013中不会加载DB Project

Visual Studio代码将不会加载tkinter包

调试时Visual Studio 2017无法检查Razor页面视图上的变量值

在MVC 5视图中无法解析Razor引用Visual Studio 2015 Enterprise

Visual Studio“无法解析库包含路径”(Visual Micro)

Visual Studio 2017无法加载项目

Visual Studio发布,无法加载“ PublishTelemetry”任务

Visual Studio 2012无法加载DLL

在单击之前,Visual Studio引用无法解析

Visual Composer无法解析

无法启动visual studio

Visual Studio Code 32位将不会加载Ubuntu Mate 17.10.1

使用Visual Studio从Blazor中的模型生成视图/页面

Visual Studio:无法编译 aspx 页面,因为无法启动 iisexpress

Visual Studio 2012:无法加载组件dll无法启动程序

Visual Studio扩展未加载

Visual Studio 2013无法识别对我的新模型的引用?

无法在Visual Studio 2019中部署SSAS表格模型

Visual Studio 2012项目:无法显示页面

无法在Visual Studio 2010中看到动态创建的页面

无法启动Visual Studio加载项示例Woodgrove Expenses

Visual Studio 2013更新3,Designer无法加载

Visual Studio 文件未找到:无法加载程序集

Visual Studio Code 无法加载 PSDesiredStateConfiguration 以外的 DSC 模块

无法安装Qt 5 Visual Studio加载项

Visual Studio 2015 C#XAML设计器无法加载

无法在Visual Studio 2015中加载C项目

Visual Studio代码错误-无法加载jshint库

Visual Studio 2017无法从指定路径加载AjaxMin MSBuild任务