错误1无法找到类型或名称空间名称“控制器”(您是否缺少使用指令或程序集引用?)

时间:2022-05-06 05:36:33

When I try to build the project in asp.net mvc3. 27 errors appearing, saying that the mvc related classes dont exist:

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

Here is an example of one:

这里有一个例子:

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();
        }
    }
}

Errors:

错误:

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

6 个解决方案

#1


18  

You will need to add System.Web.Mvc to your project references.

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

  1. Right click on References
  2. 右键单击引用
  3. Click "Add Reference..."
  4. 点击“添加参考……”
  5. Assemblies > Framework
  6. 程序集>框架
  7. Search Assemblies (Ctrl+E) for System.Web.Mvc (you will probably have more: version 2/3/4)
  8. 搜索程序集(Ctrl+E)用于System.Web。Mvc(您可能会有更多:版本2/3/4)

#2


7  

Today i have got the same type of error. I had MVC version 3 installed and the project had given error, was running without error previously. What i have done today is, I have gone for windows update automatically. In that update MVC version 3 update was downloaded and installed automatically. So the MVC version 3 reference, that was added before in my project, was no longer valid after windows update installed. So, I deleted that reference and added same reference. That fixed my error. :)

今天我犯了同样的错误。我已经安装了MVC版本3,并且项目已经给出了错误,之前运行的时候没有错误。我今天所做的是,我已经自动更新了windows。在这个更新中,MVC版本3的更新被自动下载和安装。因此,在我的项目中添加的MVC版本3引用在安装了windows update之后不再有效。因此,我删除了这个引用,并添加了相同的引用。修正我的错误。:)

#3


4  

Remove using System.Web.Mvc.Controller;. The using clause is used to define namespaces, not classes:

删除使用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();
        }
    }
}

Also make sure that you have the System.Web.Mvc assembly referenced in your project.

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

Another thing to be careful with is your namespace. I see that you are using namespace Controllers instead of namespace AppName.Controllers. Make sure that you don't have a namespace Controller (without an s) defined somewhere in your project which might conflict with the Controller class that you are trying to use here.

另一件需要注意的事情是命名空间。我看到您使用的是名称空间控制器,而不是名称空间appname.controller。确保您的项目中没有定义名称空间控制器(没有s),这可能与您要在这里使用的控制器类相冲突。

#4


1  

Are you running an old MVC (1,2,3..) framework project on a new version of visual studio (VS 2013, VS 2015)?

您是否正在运行一个新版本的visual studio (VS 2013, VS 2015)的旧MVC(1,2,3..)框架项目?

If so, plz read this solution: How do I open an old MVC project in Visual Studio 2012 or Visual Studio 2013?

如果是这样,plz阅读这个解决方案:我如何在Visual Studio 2012或Visual Studio 2013中打开一个旧MVC项目?

#5


1  

It seems you have test project in the solution. There are two ways of resolving these errors

看起来您在解决方案中有测试项目。有两种方法可以解决这些错误。

1)Exclude unit test project from the solution.

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

2)You just need to add reference of your MVC project into test project. Go to Unit Test project > Right Click on References > click on Add reference > Click on Projects Tab > Add project reference

2)您只需要将您的MVC项目的引用添加到测试项目中。转到单元测试项目>右击参考>点击添加参考>点击项目标签>添加项目参考。

Here you go !!!

在这里你去! ! !

#6


-1  

You will get this in Add Reference-> Extentions -> System.Web.Mvc

您将在Add Reference->扩展-> System.Web.Mvc中得到这一点。

#1


18  

You will need to add System.Web.Mvc to your project references.

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

  1. Right click on References
  2. 右键单击引用
  3. Click "Add Reference..."
  4. 点击“添加参考……”
  5. Assemblies > Framework
  6. 程序集>框架
  7. Search Assemblies (Ctrl+E) for System.Web.Mvc (you will probably have more: version 2/3/4)
  8. 搜索程序集(Ctrl+E)用于System.Web。Mvc(您可能会有更多:版本2/3/4)

#2


7  

Today i have got the same type of error. I had MVC version 3 installed and the project had given error, was running without error previously. What i have done today is, I have gone for windows update automatically. In that update MVC version 3 update was downloaded and installed automatically. So the MVC version 3 reference, that was added before in my project, was no longer valid after windows update installed. So, I deleted that reference and added same reference. That fixed my error. :)

今天我犯了同样的错误。我已经安装了MVC版本3,并且项目已经给出了错误,之前运行的时候没有错误。我今天所做的是,我已经自动更新了windows。在这个更新中,MVC版本3的更新被自动下载和安装。因此,在我的项目中添加的MVC版本3引用在安装了windows update之后不再有效。因此,我删除了这个引用,并添加了相同的引用。修正我的错误。:)

#3


4  

Remove using System.Web.Mvc.Controller;. The using clause is used to define namespaces, not classes:

删除使用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();
        }
    }
}

Also make sure that you have the System.Web.Mvc assembly referenced in your project.

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

Another thing to be careful with is your namespace. I see that you are using namespace Controllers instead of namespace AppName.Controllers. Make sure that you don't have a namespace Controller (without an s) defined somewhere in your project which might conflict with the Controller class that you are trying to use here.

另一件需要注意的事情是命名空间。我看到您使用的是名称空间控制器,而不是名称空间appname.controller。确保您的项目中没有定义名称空间控制器(没有s),这可能与您要在这里使用的控制器类相冲突。

#4


1  

Are you running an old MVC (1,2,3..) framework project on a new version of visual studio (VS 2013, VS 2015)?

您是否正在运行一个新版本的visual studio (VS 2013, VS 2015)的旧MVC(1,2,3..)框架项目?

If so, plz read this solution: How do I open an old MVC project in Visual Studio 2012 or Visual Studio 2013?

如果是这样,plz阅读这个解决方案:我如何在Visual Studio 2012或Visual Studio 2013中打开一个旧MVC项目?

#5


1  

It seems you have test project in the solution. There are two ways of resolving these errors

看起来您在解决方案中有测试项目。有两种方法可以解决这些错误。

1)Exclude unit test project from the solution.

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

2)You just need to add reference of your MVC project into test project. Go to Unit Test project > Right Click on References > click on Add reference > Click on Projects Tab > Add project reference

2)您只需要将您的MVC项目的引用添加到测试项目中。转到单元测试项目>右击参考>点击添加参考>点击项目标签>添加项目参考。

Here you go !!!

在这里你去! ! !

#6


-1  

You will get this in Add Reference-> Extentions -> System.Web.Mvc

您将在Add Reference->扩展-> System.Web.Mvc中得到这一点。