【问题标题】:"The type or namespace name could not be found" when publishing ASP .NET MVC Project发布 ASP .NET MVC 项目时“找不到类型或命名空间名称”
【发布时间】:2015-02-26 14:52:50
【问题描述】:

我是 C# ASP .NET MVC 的新手,所以我不确定很多事情。但是我在我的新公司继承了这个项目,我只是想构建我存储在我在这里开始时接管的计算机上的文件。我很确定自从最后一个人离开后没有人接触过这些文件,所以我认为代码应该没有问题,因为该网站现在已经上线并且正在运行。

但是当我尝试将这些文件发布到临时站点(出于测试目的)时,我得到了一堆相同的错误(删除了错误中给出的完整路径)并且项目无法发布。

    Error   77  The type or namespace name 'Controller' could not be found (are you missing a using directive or an assembly reference?)    \Controllers\AccountController.cs   15  38  Project
Error   99  The type or namespace name 'HttpGetAttribute' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   17  10  Project
Error   100 The type or namespace name 'HttpGet' could not be found (are you missing a using directive or an assembly reference?)   \Controllers\AccountController.cs   17  10  Project
Error   115 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   18  16  Project
Error   182 The type or namespace name 'HttpPostAttribute' could not be found (are you missing a using directive or an assembly reference?) \Controllers\AccountController.cs   40  10  Project
Error   183 The type or namespace name 'HttpPost' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   40  10  Project
Error   186 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   41  16  Project
Error   257 The type or namespace name 'HttpGetAttribute' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   78  10  Project
Error   258 The type or namespace name 'HttpGet' could not be found (are you missing a using directive or an assembly reference?)   \Controllers\AccountController.cs   78  10  Project
Error   265 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   79  16  Project
Error   284 The type or namespace name 'HttpPostAttribute' could not be found (are you missing a using directive or an assembly reference?) \Controllers\AccountController.cs   89  10  Project
Error   285 The type or namespace name 'HttpPost' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   89  10  Project
Error   291 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   90  16  Project
Error   332 The type or namespace name 'AuthorizeAttribute' could not be found (are you missing a using directive or an assembly reference?)    \Controllers\AccountController.cs   112 10  Project
Error   333 The type or namespace name 'Authorize' could not be found (are you missing a using directive or an assembly reference?) \Controllers\AccountController.cs   112 10  Project
Error   335 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   113 16  Project
Error   343 The type or namespace name 'AuthorizeAttribute' could not be found (are you missing a using directive or an assembly reference?)    \Controllers\AccountController.cs   121 10  Project
Error   344 The type or namespace name 'Authorize' could not be found (are you missing a using directive or an assembly reference?) \Controllers\AccountController.cs   121 10  Project
Error   345 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   122 16  Project
Error   363 The type or namespace name 'HttpGetAttribute' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   150 10  Project
Error   364 The type or namespace name 'HttpGet' could not be found (are you missing a using directive or an assembly reference?)   \Controllers\AccountController.cs   150 10  Project
Error   365 The type or namespace name 'AuthorizeAttribute' could not be found (are you missing a using directive or an assembly reference?)    \Controllers\AccountController.cs   151 10  Project
Error   366 The type or namespace name 'Authorize' could not be found (are you missing a using directive or an assembly reference?) \Controllers\AccountController.cs   151 10  Project
Error   367 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   152 16  Project
Error   377 The type or namespace name 'HttpGetAttribute' could not be found (are you missing a using directive or an assembly reference?)  \Controllers\AccountController.cs   162 10  Project
Error   378 The type or namespace name 'HttpGet' could not be found (are you missing a using directive or an assembly reference?)   \Controllers\AccountController.cs   162 10  Project
Error   379 The type or namespace name 'AuthorizeAttribute' could not be found (are you missing a using directive or an assembly reference?)    \Controllers\AccountController.cs   163 10  Project
Error   380 The type or namespace name 'Authorize' could not be found (are you missing a using directive or an assembly reference?) \Controllers\AccountController.cs   163 10  Project
Error   384 The type or namespace name 'ActionNameAttribute' could not be found (are you missing a using directive or an assembly reference?)   \Controllers\AccountController.cs   164 10  Project
Error   385 The type or namespace name 'ActionName' could not be found (are you missing a using directive or an assembly reference?)    \Controllers\AccountController.cs   164 10  Project

对于项目中的许多文件,还有许多相同类型的错误。错误的显示方式让我相信这是一个简单的修复,因为它们几乎都是相同的错误。

有人知道如何修复这些错误,以便我可以成功构建这个项目吗?

【问题讨论】:

  • 您的项目中似乎缺少对 System.Web.Mvc DLL 的引用。
  • 在您的参考文献中检查损坏的参考文献并重新导入它们。

标签: c# asp.net asp.net-mvc visual-studio-2010 compiler-errors


【解决方案1】:

您需要确保您的项目引用了所有必需的命名空间。例如,HttpGetAttribute 和该列表中的所有其他人实际上属于 System.Web.Mvc

如果您在 VS 中打开该项目的引用“文件夹”,您可能会在某些引用旁边看到警告标志。删除它们并重新添加它们。

要重新添加参考,首先将其删除,然后右键单击“参考”文件夹,然后单击“添加参考”。单击左侧的 Assembles 树视图项,然后在右侧的搜索框中输入 System.Web.Mvc,它会显示该 DLL 的版本列表。

为了将来参考,每当你看到这样的东西,解决它的最好方法是只搜索找不到的类的名称,后跟“MSDN”,在你的情况下,你会搜索“HttpGetAttribute” msdn'。这将告诉您需要包含哪些参考。

【讨论】:

  • 在哪里可以找到引用文件夹?它是否在某个项目的文件夹结构中?还是在某个地方的 VS 设置中?
  • 它将位于项目文件夹结构的顶部附近:)
  • 右键单击“参考”文件夹,然后单击“添加参考”。单击左侧的 Assembles treeview 项,然后在右侧的搜索框中键入 System.Web.Mvc,它将显示该 DLL 的版本列表。选择您想要的任何一个。你可能想要4.0.0.0
  • 在这种情况下,您想删除System.Web.WebPages 并使用版本2.0.0.0 DLL 重新添加它,或者您可以先尝试重新添加较低版本的System.Web.Mvc,看看是否有这些完全消除了所有问题。我可能会先尝试,因为更改 WebPages DLL 可能会引发更多问题。
  • 谢谢!有效。我必须添加System.Web.Mvc 3.0.0.1 才能使其工作。感谢您的帮助/耐心。
【解决方案2】:

首先确保基础项目(没有对其他项目的任何引用)构建正常。如果没有,请检查项目树中的 References 文件夹。如果任何图标有警告,则您对库的引用无效。

然后从基础项目转到引用基础项目的项目,并继续进行,直到整个解决方案再次正常构建。

【讨论】:

    【解决方案3】:

    这个问题很可能是由于 nuget 包没有被恢复。如果您的团队没有将 nuget 包检查到源代码管理中,则需要在解决方案资源管理器中右键单击解决方案并选择“启用 Nuget 包还原”,然后重新构建解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 2017-07-12
      • 2011-05-13
      相关资源
      最近更新 更多