错误1找不到类型或命名空间名称"Controller"(您是否缺少using指令或程序集引用?)

Dmi*_*iyd 12 asp.net c#-4.0 asp.net-mvc-3

当我尝试在asp.net mvc3中构建项目时.出现27个错误,说mvc相关类不存在:

这是一个例子:

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
Run Code Online (Sandbox Code Playgroud)

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

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
Run Code Online (Sandbox Code Playgroud)

小智 21

您需要添加System.Web.Mvc到项目引用中.

  1. 右键单击References
  2. 点击"添加参考..."
  3. 装配>框架
  4. 搜索程序集(Ctrl+ E)System.Web.Mvc(您可能会有更多:版本2/3/4)


小智 8

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


Dar*_*rov 4

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

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

还要确保您System.Web.Mvc的项目中引用了该程序集。

另一件需要小心的事情是你的命名空间。我发现您正在使用namespace Controllers而不是namespace AppName.Controllers. 确保您没有在项目中的某处定义了namespace Controller(没有),这可能与您在此处尝试使用的类发生冲突。sController