Recently, I was working on an ASP.NET MVC project using Visual Studio and the web project had strongly-typed views that were using model classes located in another class project in the same solution. The project itself got built successfully and was running fine until it tried to show those views, it returned the Compilation Error saying that the type or namespace name could not be found. In this article, I will show you what I tried and how I finally solved the problem.
The exact server error is shown below. The 'MyCustomClass' is the assembly name of the class project in the solution.
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'MyCustomClass' could not be found (are you missing a using directive or an assembly reference?)
It seemed strange to me that the view had trouble finding the reference of the type since the view was strongly-typed, generated by Visual Studio using "Add View" window and the class from another assembly was chosen from "Model Class" drop-down menu.
What was also odd was that the error was occurring in auto-generated view code and not from the source file in the solution.
Here are the things I tried to solve the problem.
Checking "Copy Local" property of the reference
The first thing I checked was if the class library's reference in the ASP.NET MVC project had the "Copy Local" property set to true. In the past, I had some issues with Visual Studio not finding types when the property was set to false.
We can check this property by expanding the References node in the ASP.NET MVC project and right-click the reference in question and select Properties from the context-menu as shown below:
Then check the "Copy Local" in the Properties window:
In my case, it was already true, so I had to keep looking.
Adding namespace in web.config of the View
Next, I tried to add the namespace that it couldn't find manually inside the
<namespaces> tag in the
web.config file under Views folder (not in the one located in the root folder!!):
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
This also didn't do anything.
To learn more about why there is also a
folder, check this
Adding @Using inside the View
Next, I tried to add namespace directly to the view itself that is causing the problem. I added the
@using directive at the beginning of the view:
This modification changed the source of the error from the auto-generated code to the source file of the view itself and nothing more.
Checking properties of the class project
Then I turned my attention to the class project itself to see if anything in there might cause this "type or namespace name could not be found" error.
First, I checked the Properties of the project. You do that by right-clicking on the project name in solution explorer and selecting "Properties" from the context-menu, as shown below:
Click to Enlarge
Instantly, I noticed a possible culprit. Under the Application tab, the "Output type" had "Console application" selected instead of "Class Library":
Click to Enlarge
In the end, this was the source of the problem. Changing "Output type" to "Class Library" finally fixed the issue.
When the ASP.NET MVC project builds and the web application runs successfully, the run-time error The type or namespace name could not be found might come as a surprise. In this article, we examined a few examples, what to check if this problem occurs. In the end, for me, the cause of the problem was a referenced class library, that was mistakenly created as a console application.