Classes residing in App_Code is not accessible

Viewed 144488

I have created a website in ASP.NET and have created a class and put it inside of the App_Code folder. However I cannot access this from my other pages. Does something need to be configured to allow this? I have made it work in previous projects, but not in this one, somehow.

namespace CLIck10.App_Code
{
    public static class Glob
    {
        ...
    }
}
8 Answers

Right click on the .cs file in the App_Code folder and check its properties.

Make sure the "Build Action" is set to "Compile".

Put this at the top of the other files where you want to access the class:

using CLIck10.App_Code;

OR access the class from other files like this:

CLIck10.App_Code.Glob

Not sure if that's your issue or not but if you were new to C# then this is an easy one to get tripped up on.

Update: I recently found that if I add an App_Code folder to a project, then I must close/reopen Visual Studio for it to properly recognize this "special" folder.

make sure that you are using the same namespace as your pages

I wanted to add an answer for why this happened to me, I just used a newer version of Visual Studio to add to an older project, and used "Add Web Form". I have a class in App_Code, which for some reason, on that set of pages only, was not accessible. I tried changing the class to "Compile", and suddenly the rest of the project complained about the class being in multiple places, which it is not (also with a number at the beginning like 3_Whatever).

I unloaded the project file, and edited it manually, and found that even though the properties window said "Content", the directive was "Compile". Also, it had not added the SubType, which all others had. Lastly, the aspx page had "CodeBehind" instead of "CodeFile" which I changed as well, unsure if I needed to but at least it looks like the rest of them now!

<Content Include="MyFile.aspx.cs">
  <DependentUpon>MyFile.aspx</DependentUpon>
  <SubType>ASPXCodeBehind</SubType>
</Content>

Everything works now.

Related