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!
    }
  }
}
Discombobulous
  • 1,112
  • 2
  • 14
  • 25
  • Have you modified the namespace of the ClassA?, as by default it append the folder name(.App+Code) in Namespace. – Jha.prashant Dec 19 '13 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). – Luaan Dec 19 '13 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. – mwilson Dec 19 '13 at 17:36
  • Please check this post, It will help you: [App_Code and App_Data][1] [1]: http://stackoverflow.com/questions/7480437/asp-net-app-data-app-code-folders – Jha.prashant Dec 19 '13 at 17:40

2 Answers2

7

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.

Jha.prashant
  • 233
  • 1
  • 8
  • 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. – Discombobulous Dec 19 '13 at 17:51
  • Eric@nothing else was needed. Your namespace and code was good. – Jha.prashant Dec 19 '13 at 18:32
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.

Simon Martin
  • 4,203
  • 7
  • 56
  • 93
Alok
  • 1,290
  • 1
  • 11
  • 21
  • 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) – Discombobulous Dec 19 '13 at 17:32
  • @EricChen Please update your question with the current code you're using. – mason Dec 19 '13 at 17:35