【问题标题】:How to fix error The type or namespace name 'Linq' does not exist in the namespace 'System'如何修复错误命名空间“System”中不存在类型或命名空间名称“Linq”
【发布时间】:2015-12-10 08:37:21
【问题描述】:

ASP.NET MVC4 应用程序在包含 Razor 帮助程序的 App_Code 文件夹中包含 Helper.chtml 文件。 升级到 Visual Studio 社区版后编译报错

Error   CS0234  The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\user\AppData\Local\Temp\Temporary ASP.NET Files\admin\e209c0e0\c74032ba\Sources_App_Code\helper.cshtml.72cecc2a.cs 16

开始发生。生成的代码包含:

namespace ASP {
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;  // Error occurs in this line
    using System.Net;
    using System.Web;
    using System.Web.Helpers;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.WebPages;
    using System.Web.WebPages.Html;

    #line 1 "C:\Web\Erp\App_Code\Helper.cshtml"
    using System.Web.Mvc;

    #line default
    #line hidden

    #line 2 "C:\Web\Erp\App_Code\Helper.cshtml"
    using System.Threading;

    #line default
    #line hidden

    #line 3 "C:\Web\Erp\App_Code\Helper.cshtml"
    using MyApp.Business;

    #line default
    #line hidden


    public class Helper : System.Web.WebPages.HelperPage {

错误指向using System.Linq; 行。

如何解决这个问题? 使用 ASP.NET4

我尝试向项目添加对 System.Core 的引用,但出现错误,表明它已被引用。 web.confing 包含

<compilation debug="true">
</compilation>

我改成

<compilation debug="true" targetFramework="4.0">
</compilation>

根据The type or namespace name 'Linq' does not exist in the namespace 'System'https://social.msdn.microsoft.com/Forums/en-US/c970ea3d-5238-4ec7-8859-e16b40dccd3d/the-type-or-namespace-name-linq-does-not-exist-in-the-namespace-systemare-you-missing-an?forum=linqprojectgeneral

但之后编译错误变为

Error   CS0234  The type or namespace name 'global_asax' does not exist in the namespace 'ASP' (are you missing an assembly reference?)

指向代码

    protected static ASP.global_asax ApplicationInstance {
        get {
            return ((ASP.global_asax)(Context.ApplicationInstance));
        }
    }

在生成的 helper.cshtml.72cecc2a.cs 文件中。

如何解决这个错误?

更新

web.config 的相关部分是:

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <system.data>
    <DbProviderFactories>
      <clear />
      <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>

  <system.web>
    <httpModules>
      <remove name="RoleManager" />
    </httpModules>

    <compilation debug="true">
    </compilation>

    <customErrors mode="Off" />
    <globalization uiCulture="auto" culture="auto" enableClientBasedCulture="true" />
    <pages>
      <namespaces>
        <add namespace="MYApp.Business" />
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

【问题讨论】:

  • 由于您已经在使用 ASP.Net V4,因此可能不需要应用共享链接中建议的分辨率。尝试删除System的引用并重新添加。
  • 我删除并重新添加了对系统的引用,但问题仍然存在。
  • @Yogi 我更新了 queston 并添加了主要的 web.config 内容。也许这个或 Views\web.config 可以改变来解决这个问题
  • 我猜您可以在 web.config 的 pages 部分添加系统命名空间引用。 ` `
  • 我将 &lt;add namespace="System" /&gt; 添加到主 web.config 和 Views/web.config 但问题仍然存在

标签: asp.net-mvc visual-studio asp.net-mvc-4 razor razor-declarative-helpers


【解决方案1】:

尝试在默认控制器中添加 System.Linq 命名空间。

【讨论】:

  • 什么是默认控制器? App有控制器的通用基类,应该加在那里吗?
  • 正是这个控制着你的行动。例如默认情况下,Action Controller 控制您的登录和注册操作。
  • 也请检查这篇文章和评论部分,它可能会有所帮助。 stackoverflow.com/a/22873162/5600402 在更新 VS 时,您的某些引用似乎没有更新。
  • 这不是缺少使用文件或缺少参考的问题 - 我已经在我的开发机器上与这个问题作斗争了几个月。我最终使用了一台干净的 Windows 机器,除了安装了 VS pro 2019 之外什么都没有,项目在这里编译。这不是一个可行的解决方案,因为所有其他团队成员在他们的开发机器中不断收到此错误。潜在的问题比愚蠢的语法问题要深得多。
  • 我什至在创建一个全新的项目时遇到了这个问题,在 VS 添加新项目指南的一个干净的解决方案中。每个视图上都有相同的错误错误消息。
猜你喜欢
  • 2012-02-22
  • 2016-10-08
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多