2012-01-17 150 views
8

当我尝试在asp.net mvc3中构建项目时。 27个错误出现,说是MVC相关类不存在:错误无法找到类型或名称空间名称'Controller'(您是否缺少using指令或程序集引用?)

这里是一个示例:

Error 1 The type or namespace name 'Controller' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Controllers\HomeController.cs 10 35 MvcApplication1 

UPDATE

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace MvcApplication2.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      ViewBag.Message = "Welcome to ASP.NET MVC!"; 

      return View(); 
     } 

     public ActionResult About() 
     { 
      return View(); 
     } 
    } 
} 

错误:

Error 1 The type or namespace name 'Controller' could not be found (are you missing a using directive or an assembly reference?) c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 9 35 MvcApplication2 

Error 2 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?) c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 11 16 MvcApplication2 


Error 3 The name 'ViewBag' does not exist in the current context c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 13 13 MvcApplication2 


Error 4 The name 'View' does not exist in the current context c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 15 20 MvcApplication2 


Error 5 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?) c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 18 16 MvcApplication2 


Error 6 The name 'View' does not exist in the current context c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 20 20 MvcApplication2 
+0

请确保您包含System.Web.MVC和System.Web.Mvc.Controller或不意外删除了MVC命名空间的使用条款 – Devjosh 2012-01-17 10:40:58

+0

我没有认出控制器 – 2012-01-17 10:52:23

+0

在这种情况下请查看此链接if它有助于http://stackoverflow.com/questions/6238295/the-type-or-namespace-name-servicecontroller-could-not-be-found @dmitry – Devjosh 2012-01-17 11:00:04

回答

4

删除using System.Web.Mvc.Controller; 。在使用子句用于定义命名空间,而不是类:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      return View(); 
     } 

     public ActionResult About() 
     { 
      return View(); 
     } 
    } 
} 

另外,还要确保你有System.Web.Mvc装配在你的项目中引用。

要小心的另一件事是你的名字空间。我看到您正在使用namespace Controllers而不是namespace AppName.Controllers。确保您的项目中没有定义namespace Controller(没有s),这可能与您在此尝试使用的Controller类相冲突。

+0

告诉你事实上,当我尝试添加参考时,无法找到System.web.mvc参考 – 2012-01-17 11:02:56

+1

@ DmitryMakovetskiyd,你有没有安装ASP.NET MVC 3?还要确保你的目标是完整的.NET 4.0,而不是.NET 4.0客户端配置文件。 – 2012-01-17 11:49:36

17

您需要将System.Web.Mvc添加到您的项目参考中。

  1. 上的引用右键单击
  2. 点击System.Web.Mvc “添加引用...”
  3. 大会>框架
  4. 搜索组件(按Ctrl +Ë)(你会可能有更多:版本2/3/4)
+0

在我删除System.Web.Mvc的旧引用并重建解决方案并通过上述提及过程再次添加之后,这对我有用。 – Jamil 2016-05-19 09:56:45

7

今天我得到了相同类型的错误。我安装了MVC版本3,并且项目发生了错误,之前运行时没有错误。 我今天所做的是,我已经自动更新了Windows更新。在该更新中,MVC版本3更新被下载并自动安装。因此,之前在我的项目中添加的MVC版本3引用在安装Windows Update后不再有效。 因此,我删除了该引用并添加了相同的引用。这解决了我的错误。 :)

-1

你会得到这个Add Reference-> Extentions -> System.Web.Mvc

1

看来你有解决方案的测试项目。 解决这些错误的方法有两种:

1)从解决方案中排除单元测试项目。

2)您只需将您的MVC项目的引用添加到测试项目中。 转到单元测试项目>右键单击参考>单击添加参考>单击项目选项卡>添加项目参考

在这里你去!