"The type or namespace name could not be found" 发布时 ASP .NET MVC 项目

"The type or namespace name could not be found" when publishing ASP .NET MVC Project

我是 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

项目中的很多文件出现了更多的同类错误。错误显示的方式让我相信这是一个简单的一次性修复,因为它们几乎都是相同的错误。

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

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

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

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

为了以后的参考,每当你看到这样的东西时,最好的解决方法是只搜索找不到的 class 的名称,然后是 'MSDN',在您会搜索您的案例 'HttpGetAttribute msdn'。这将告诉您需要为它包括哪个参考。

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

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

问题很可能是由于 nuget 包没有被恢复。如果您的团队没有将 nuget 包签入源代码管理,您将需要右键单击解决方案资源管理器中的解决方案和 select 'Enable Nuget Package Restore',然后重建解决方案。