Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio cannot detect newly created class for asp.net "website"

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!
    }
  }
}
like image 400
Discombobulous Avatar asked Dec 26 '22 16:12

Discombobulous


1 Answers

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.

like image 143
Jha.prashant Avatar answered May 05 '23 04:05

Jha.prashant