Error 1 The type or namespace name 'Controller

2020-06-11 14:57发布

问题:

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

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

回答1:

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

  1. Right click on References
  2. Click "Add Reference..."
  3. Assemblies > Framework
  4. Search Assemblies (Ctrl+E) for System.Web.Mvc (you will probably have more: version 2/3/4)


回答2:

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. :)



回答3:

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

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.

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.



回答4:

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

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



回答5:

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.

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

Here you go !!!



回答6:

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