Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 1 The type or namespace name 'Controller' could not be found (are you missing a using directive or an assembly reference?)

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
like image 596
Dmitry Makovetskiyd Avatar asked Jan 17 '12 10:01

Dmitry Makovetskiyd


People also ask

How do you fix the type or namespace name could not be found?

That may be it's in a different project in the current solution, in a external DLL, or just in a different namespace in another file in the current project. Start by hovering the mouse over the error, and open the drop down that appears. If it has an "include" option, click that, and it should fix it.

Why am I getting error CS0246 the type or namespace name could not be found?

The following situations cause compiler error CS0246. Did you misspell the name of the type or namespace? Without the correct name, the compiler cannot find the definition for the type or namespace. This often occurs because the casing used in the name of the type is not correct.


5 Answers

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)
like image 90
Eldad Avatar answered Oct 05 '22 04:10

Eldad


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

like image 33
Subhajit Ray Avatar answered Oct 05 '22 03:10

Subhajit Ray


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.

like image 23
Darin Dimitrov Avatar answered Oct 05 '22 02:10

Darin Dimitrov


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?

like image 23
Dudi Avatar answered Oct 05 '22 04:10

Dudi


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 !!!

like image 23
Ashish Meshram Avatar answered Oct 05 '22 02:10

Ashish Meshram