Classes residing in App_Code is not accessible
Asked Answered
T

8

215

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
    {
        ...
    }
}
Tafia answered 3/8, 2009 at 13:12 Comment(2)
I assume you're including the namespace in your web pages?Colby
Possible duplicate - #409570Jamesy
T
704

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

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

Tertiary answered 3/8, 2009 at 13:14 Comment(15)
How can you set this as the default build action? or do I have to do that for every file in the App_Code folder?Tafia
@Tafia - it should be the default action on that folder. How are you adding classes to it?Restrictive
This appears to be a bug in VS2012. Happens to me too.Extempore
Thanks a lot! this saved me, It weird why files in app_code would default to "content" though.Ferrick
for those that want more details you can find at: how-to-code-net.blogspot.ro/2014/04/…Housefly
VS 2015, Its very frustrating!Castro
Yay this solved 142 errors in my Visual Studio project, thanks so much for sharing this solution :-]Holy
Spent like 2 hours trying to solve this in VS2015 and then found this, dear lord I hate Visual Studio. Thanks!Reese
Happening in VS 2017! But only for Web FormsUniformity
my problem was somewhat different but it solved using your suggestion, I had a view with cshtml and cs files in app_code, and for some reason cs file had build action set to content which is why i could not get the helper view (inside app_code) to be used in other views.Scofflaw
Still happening in VS 2019 . thanks for this solution.Exosphere
And it is still an issue in VS 2022. Thank you for the solution.Kiarakibble
this was great explanation after 4 year finally I got a solution. thanks kevin and stevenGeraldine
Similar problem when copy/paste .cs file on App_code folder in VS 2022, thanksRaulrausch
Thanks Stephen McD and Alexa Adrian. I hadn't probably wrote code for App_Code in years - I usually put things somewhere else - or in a dll. I had forgotten maybe some about App_Code.Repercussion
M
14

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.

Melmela answered 3/8, 2009 at 13:15 Comment(3)
It was something to do with the build action by the look of it - FailBoy's answer solved the problem. Thanks for replying anyway :)Tafia
Nice one that worked as I added a namespace to the class in the App_Code folder.Domash
Why do we have to include the namspace like that? Why intelliSense is not able to access that class without specifying the name of folder?Voletta
C
11

Go to the page from where you want to access the App_code class, and then add the namespace of the app_code class. You need to provide a using statement, as follows:

using WebApplication3.App_Code;

After that, you will need to go to the app_code class property and set the 'Build Action' to 'Compile'.

Carbajal answered 28/2, 2014 at 6:42 Comment(0)
J
6

I haven't figured out yet why this occurs, but I had classes that were in my App_Code folder that were calling methods in each other, and were fine in doing this when I built a .NET 4.5.2 project, but then I had to revert it to 4.0 as the target server wasn't getting upgraded. That's when I found this problem (after fixing the langversion in my web.config from 6 to 5... another story)....

One of my methods kept having an error like:

The type X.Y conflicts with the imported type X.Y in MyProject.DLL

All of my classes were already set to "Compile" in their properties, as suggested on the accepted answer here, and each had a common namespace that was the same, and each had using MyNamespace; at the top of each class.

I found that if I just moved the offending classes that had to call methods in each other to another, standard folder named something other than "App_Code", they stopped having this conflict issue.

Note: If you create a standard folder called "AppCode", move your classes into it, delete the "App_Code" folder, then rename "AppCode" to "App_Code", your problems will return. It doesn't matter if you use the "New Folder" or "Add ASP .NET Folder" option to create "App_Code" - it seems to key in on the name.

Maybe this is just a .NET 4.0 (and possibly earlier) issue... I was just fine in 4.5.2 before having to revert!

Jamesy answered 14/6, 2016 at 17:0 Comment(1)
Realizing that the App_Code folder was a special name and I don't think that I need any project magic, I just renamed it to ApplicationCode and everything works fine now.Servomechanism
P
4

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

Pocky answered 3/8, 2009 at 14:51 Comment(2)
But why when it's in APP_CODE? I thought anything in there with PUBLIC classes should be exposed throughout the solution automatically!?Bogan
A namespace is not required in the classes, and I don't use namespace for my universal classes.Kiarakibble
I
1

In my case, I couldn't get a project to build with classes defined in the App_Code folder.

Can't replicate the scenario precisely to comment, but had to close and re-open visual studio for intellisense to co-operate agree...

I noticed that when a class in the App_Code folder is set to 'Compile' instead of 'Content' (right-click it) that the errors were coming from a second version of the class... Look to the left-most of the 3 of the 3 fields between the code pane and the tab. The 'other' one was called something along the lines of 10_App_Code or similar.

To rectify the issue, I renamed the folder from App_Code to Code, explicitly set namespaces on the classes and set all of the classes to 'Compile'

Intermediate answered 5/6, 2017 at 14:51 Comment(0)
T
0

I found it easier to move the files into a separate Class Library project and then reference that project in the web project and apply the namespace in the using section of the file. For some reason the other solutions were not working for me, but this work around did.

Thrum answered 10/6, 2016 at 14:48 Comment(0)
O
0

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.

Ousel answered 9/6, 2022 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.