C# – Class file doesn’t compile outside of App_Code folder

asp.netcnetvisual studio 2010visual-web-developer-2010

I am using Visual Web Developer Express 2010 edition.
I have chosen "New Website" (and not New Project) to build a website.
My project is targeted for .NET 2.0

I have a class file which I originally created under the App_Code folder. I instantiated an object of the class in my default.aspx.cs

MyClass m = new MyClass();

This compiles fine. However, the moment I deploy it on my remote IIS Server, it gives a compilation error when I invoke the remote website from my browser.

error CS0246: The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference?)

So I found this SO Question here – The type or namespace name 'LAD' could not be found (are you missing a using directive or an assembly reference?)

It says that I shouldn't put the class file into the App_Code directory. It asks to put it in a diff place and set the Build Action (in Properties) to Compile. I couldn't find a Build Action in Properties in "MS Visual Web Developer 2010" – so I had to skip that step.

So just I deleted it from App_Code and recreated it in the root folder. Now my project doesn't build at all – I get the same error on the build phase itself.

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

I have a strong feeling my MyClass.cs doesn't get built at all. What do I do? How do I tell the IDE to build it?

Best Answer

Follow this step:

1) Right click on your class file in App_Code folder.
2) Click on properties.
3) Change Build Action to Compile
4) Build your App.

Check now it works.