Reputation: 33720
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
{
...
}
}
Upvotes: 216
Views: 153734
Reputation: 7545
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!
Upvotes: 6
Reputation: 1042
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.
Upvotes: 0
Reputation: 857
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'
Upvotes: 1
Reputation: 38392
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.
Upvotes: 14
Reputation: 2603
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.
Upvotes: 0
Reputation: 17502
Right click on the .cs
file in the App_Code
folder and check its properties.
Make sure the "Build Action" is set to "Compile".
Upvotes: 708
Reputation: 105
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'.
Upvotes: 11
Reputation:
make sure that you are using the same namespace as your pages
Upvotes: 4