Tuesday, January 3, 2012
C# | App_Code class missing in the current context
Something as simple as creating a new class in Visual Studio can be tricky.
Lately I've been creating a class by right-clicking "App_Code" in a Web Project and selecting "Add", "Class..."
When accessing the class "Tiago", I would get:
- The name 'Tiago' does not exist in the current context
- The type or namespace name 'Tiago' could not be found (are you missing a using directive or an assembly reference?)
Here are some thoughts on this:
- Adding classes to the root folder works fine
- Adding classes to the root folder, and THEN moving them to the App_Code folder works fine
- Adding classes through the "Generate class for 'X'" context menu works fine
Why is this happening?
When you create a new class within the App_Code folder, its default behavior (check by clicking properties on the class) will be "Content", when it should be "Compile".
So we can just confirm we have the right namespace for the class (since creating it on a subfolder may add extra text to it) and change the "Build Action" to "Compile" instead of "Content".