johnmcp
johnmcp

Reputation: 921

Type or namespace could not be found from App_code folder

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
    {

Upvotes: 34

Views: 44048

Answers (4)

subsci
subsci

Reputation: 1740

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

Upvotes: 82

Bashar Abu Shamaa
Bashar Abu Shamaa

Reputation: 2029

Yes, put the calsses to another folder(not asp.net special folder), and only use the main namespace for the application is solve this issue. Thanks johnmcp.

Upvotes: 2

johnmcp
johnmcp

Reputation: 921

If you convert it to a web app it should work. The downside is that it will no longer autobuild your code in app_code folder every time you change it and spin up the app. I have never seen a professional developer use a website project. I have no idea who MS were targeting when they created them.

Upvotes: 2

Coding Flow
Coding Flow

Reputation: 21881

You need to add

using EMCWebAdmin.App_Code;

to all the pages you want to be able to use the class.

Alternatively you change the namesspace that the class is in to the same one that all the web pages use which presuming it is EMCWebAdmin

then in your class change

namespace EMCWebAdmin.App_Code
    {
...

to

namespace EMCWebAdmin
    {
...

This is a feature of visual studio, if you create a class in a folder structure, it uses a namespace that follows the folder structure.

Upvotes: 5

Related Questions