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
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.
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.
You will need to add System.Web.Mvc
to your project references.
System.Web.Mvc
(you will probably have more: version 2/3/4)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. :)
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.
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?
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 !!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With