Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type or namespace could not be found from App_code folder

Tags:

c#

asp.net

I have written a class called ArchivedFilesWrapper in the App_code folder of my project, however when I use this class in another file in a different folder i get error:

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

I thought every page should be able to find classes that are contained within the same project, but I guess this is not the case. Can someone please tell me what using statement I need to have?

Here is a snippet from my class:

using System; using System.Collections.Generic; using System.Linq; using System.Web;  namespace EMCWebAdmin.App_Code {     public class ArchivedFilesWrapper     { 
like image 264
johnmcp Avatar asked Feb 07 '12 15:02

johnmcp


People also ask

What is the App_Code folder?

The App_Code folder can contain source code files written as traditional class files — that is, files with a . vb extension, . cs extension, and so on. However, it can also include files that are not explicitly in a specific programming language.

What is use of App_data and App_Code folder?

The App_Code folder can contain sub-directories of files, which can include class files that in different programming languages. Contains application data files including . mdf database files, XML files, and other data store files.


1 Answers

Perhaps the problem will be solved by changing the Build Action Property of the *.cs source file to Compile from Content. From the Solution Explorer right click on the source file and choose Property.

Note that the App_Code folder is intended for use in Web Site Projects.

Note that for a Web Application Project or MVC project, adding an App_Code folder to your project and putting *.cs files in it will cause problems. I ignorantly added an App_Code folder to my MVC project from the Solution Explorer. VS defaulted the name space to MyProjectName.App_Code. In this case Visual Studio 2012 defaulted the Build Action to Content, even though the type was .cs code. After I changed Build Action Property of the *.cs source file to Compile from Content the namespace was resolved in other folder locations of the project. However because of problems, I had to change the name of folder--see below.

Important

In the MVC or Web Application project, the App_Code folder is trouble because it has Web Site Project type semantics. This folder is compiled when published (deployed) to the server. By changing Build Action from Content to Compile, you resolve the namespace issue on your development environment by forcing immediate compilation, but you get trouble when the second compilation results in objects defined twice errors on deployment. Put the code files in a folder with a different name. If you converted a Web Site to a Web Application, see the guidelines on the Net for this--not in the scope of this question. To read more about App_Code folder in the different project types see this blog

like image 93
subsci Avatar answered Sep 30 '22 17:09

subsci