4

I am trying to modify an existing .net "website" project by adding a new class to it. I added my class inside the App_Code folder (let's call this class "ClassA"), and tried to access it from the outside in an aspx.cs file. For some reason, right after I created the class, I can create an instance of it in my aspx.cs file without any warnings from Visual Studio (e.g., ClassA a = new ClassA()). But every time I rebuild the project, I get the following Error from visual studio (The type or namespace name 'ClassA' could not be found (are you missing a using directive or an assembly reference?)). What am I missing here?

Code for Class A -> App_Code/ClassA.cs

public class ClassA
{
  public string test;
}

Code for Test.aspx.cs

namespace A{
  public class Test : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      ClassA a = new ClassA(); // line with error
    }
  }
}

If I put everything in Test.aspx.cs I get no errors:

namespace A{
  public class ClassA
  {
    public string test;
  }

  public class Test : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      ClassA a = new ClassA(); // no error!
    }
  }
}
4
  • Have you modified the namespace of the ClassA?, as by default it append the folder name(.App+Code) in Namespace. Commented Dec 19, 2013 at 17:21
  • Is it a web site or a web project? A website is not really compiled in compile-time (no output DLL), while a web project is (a single output DLL). If it's a web project and you've got code in App_Code, it's going to result in you having the class there twice (once runtime compiled from App_Code, and once again in the compiled DLL). Commented Dec 19, 2013 at 17:24
  • Have you tried deleting your Temporary ASP.NET Files folder? I've had this happen to me a few times. A restart fixed the problem in those cases. There was an open file handle on the Temporary ASP.NET files, so the old DLL's were not being removed. Commented Dec 19, 2013 at 17:36
  • Please check this post, It will help you: [App_Code and App_Data][1] [1]: stackoverflow.com/questions/7480437/… Commented Dec 19, 2013 at 17:40

2 Answers 2

8

By default the classes inside App_Code folder has property "Build Action" set as "Content".

If you want to use it you need to set its "Build Action" property to "Compile".

This should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I'm not sure if this was the solution that solved the problem, but I tried a bunch of things suggested in this thread (this solution was the last thing I tried) and it works now. Thanks.
Eric@nothing else was needed. Your namespace and code was good.
3

I am really sorry, for I could not totally understand the problem. In my opinion you should not use namespaces when you create a class in app_code and let it be accessible to the whole website project.
In this instance you have added a namespace, try adding:

using b;

where b is your namespace you used in your new class. This should solve your problem.

2 Comments

so I removed the namespace in the class definition file in App_code. I'm still getting the same error in my aspx.cs file outside of app_code (The type or namespace name 'ClassA' could not be found)
@EricChen Please update your question with the current code you're using.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.