当我尝试在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
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
您需要将System.Web.Mvc
添加到项目引用中。
System.Web.Mvc
(你可能会有更多:版本2/3/4)今天我遇到了同样的错误。我安装了MVC版本3并且项目出错,以前运行时没有错误。我今天所做的是,我已经自动进行了Windows更新。在该更新中,下载并自动安装MVC版本3更新。因此,在我的项目之前添加的MVC版本3引用在安装Windows更新后不再有效。所以,我删除了该引用并添加了相同的引用。这解决了我的错误。 :)
删除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();
}
}
}
还要确保您的项目中引用了System.Web.Mvc
程序集。
另外需要注意的是命名空间。我看到你使用namespace Controllers
而不是namespace AppName.Controllers
。确保您的项目中没有定义namespace Controller
(没有s
),这可能与您在此处尝试使用的Controller
类冲突。
您是否在新版Visual Studio(VS 2013,VS 2015)上运行旧的MVC(1,2,3 ..)框架项目?
如果是这样,请阅读此解决方案:How do I open an old MVC project in Visual Studio 2012 or Visual Studio 2013?
看来你在解决方案中有测试项目。有两种方法可以解决这些错误
1)从解决方案中排除单元测试项目。
2)您只需要将MVC项目的引用添加到测试项目中。转到单元测试项目>右键单击引用>单击添加引用>单击项目选项卡>添加项目引用
干得好 !!!
你会在Add Reference-> Extentions -> System.Web.Mvc
得到这个