Description:
I have developed a website using ASP.NET and added a class file inside the App_Code
folder. However, I am unable to access this class from other pages within the project. I’ve successfully implemented a similar structure in previous projects without issues, so I’m unsure what might be causing the problem in this case. Is there a specific configuration required to enable this?
Primary Solution: Check Build Action
-
Right-click the
.cs
file located in theApp_Code
folder. -
Select Properties.
-
Ensure the "Build Action" is set to "Compile".This allows the class to be compiled and made available throughout the application.
Additional Solutions:
2. Verify the File Is in a Web Application Project (Not Web Site Project)
-
App_Code
is typically used in Web Site Projects, not Web Application Projects. -
If you are using a Web Application Project, classes should be placed in regular folders (like
Models
orHelpers
), and included in the.csproj
file.
3. Check the Class Accessibility
-
Ensure your class is declared as
**public**
so it can be accessed from other parts of your application:public class MyClass{// Class members}
4. Clean and Rebuild the Project
-
Sometimes, build inconsistencies can cause this issue.
-
Clean the solution:
Build > Clean Solution
-
Rebuild:
Build > Rebuild Solution
-
5. Check Namespace Declarations
-
Ensure you're using the correct
using
directive at the top of your page:using YourNamespaceName;
6. Confirm There Are No Compilation Errors
-
A compile error elsewhere in the
App_Code
folder may prevent the entire folder from compiling, causing all classes within to be inaccessible.
7. Verify Target Framework Compatibility
-
Ensure the project’s target .NET Framework is appropriate and consistent with the class code.
Let me know if you'd like help identifying whether your project is a Web Site or Web Application type, or if you want assistance migrating code between them.