asp.net using different App_Code folder - asp.net

I created folder "Test" under my web site root.
Under this folder I created another App_Code folder and put my class code there.
I also add default.aspx under Test folder.
In my code behind I cannot access class code. Only classes under top App_Code is available.
How can I access code under second App_Code? I tried to add same namespace for page and class, but this does not help.

You must put all class code inside the App_Code folder from the root folder. One trick would be to create a Test folder inside the main App_Code folder. You are allowed to put as many subfolders as you need.
https://msdn.microsoft.com/en-us/library/t990ks23(v=vs.140).aspx

Related

ASP.NET App_Code in folder

I am working on a ASP.NET VB project. The host is pretty restrictive, so the App_Code folder has to be placed under the wwwroot folder.
My current folder structure (on the server) looks like this:
root
<-- Cant create folders or files here, host is restrictive.
wwwroot
App_Code
Class.VB
Styles
style.css
default.aspx
web.config
It seems to be working, if Class.vb contains errors, i get a compilation error on the website, so i know its compiling the class.
But i am unable to use the class in my other code files.
Example:
Dim emailFilter As Validation = New Validation()
I get this error in VS2010:
Type 'Validation' is not defined
How do i use the App_Code folder when its inside another folder?
Could it be an issue with namespaces? I might be more inclined to create a library for your validation class and add a reference to it instead.
I did find some similar issues to yours and they all referenced setting the Build Action for the class to 'compile'.
Classes residing in App_Code is not accessible

CS0103: The name does not exist in the current context dev to webserver issue

I have created a few classes that operate separately from the normal aspx.cs files. When I added a new class I was prompted to add these to app_data, i did. So I copied out these files and the app_data folder and it cannot be found. I tried the .cs files at the root of the website and they still could not be found.
I'm sure i'm messing up on a common mistake.
Thanks!
They need to be in the App_Code folder.

reference app_code classes in web application project

I have created an asp.net website, with the accompanying app_code folder.
In the same solution I added a web application project, and I want to use the classes that are in the website app_code folder.
I tried adding a reference, and then adding the project (the website), but the list of project is empty...
Thanks.
You are doing it wrong. Create a class library project and move all those classes from the app_code folder to the new project. Then reference this project from both website and web application project.
Very late, but sometimes classes in App_Code may need to stay in the same project, then
Create a new folder (say "code" )in web application, and add class files from app_code as links to this folder. This lets you have a single copy.
2) Ensure Build Action for each of these imported files are "Compile" in the file property folder if need be.
3) Exclude App_Code folder from web application.
More information and reasons - please see here:
http://vishaljoshi.blogspot.in/2009/07/appcode-folder-doesnt-work-with-web.html

consuming classes from folder other than App_Code

I am creating a site wherein I have a folder which contains some .cs files. I want to access those classes in .aspx and .ascx files. I’ve created some properties in it, but when I create the object of the class I don’t find that property via IntelliSense.
How can I use and consume those properties from that .cs file?
The website will only compile code files that are in the App_Code folder or are codebehind files for referenced controls. There isn't a way to reference classes defined in code files outside of the App_Code folder.
If you compile those classes and put the resulting dll in your website's bin folder, then you can reference them. To do that, you'll need to add them to a Web Application project in Visual Studio. See Ian Robinson's WAP blog post for most details.
Just have those properties public and you'll see them.
If still no luck please post your code and also tell: can you create instance of the class without error? Can you access any other properties or methods?

Page class outside of App_Code will not compile

I have a website that has 2 files as follows:
page.aspx
page.aspx.cs
It used to be that I could just drop new files onto the web server and IIS would automatically compile the files and I could access the page e.g.
http://www.website.com/page.aspx
... and the associated functionality in the page class contained in the .cs file would work nicely.
Now I get the error: "Could not load type namespace.classname" which refers to my page class.
Now for some strange reason I have to put all my .cs files, even page classes into the app_code folder.
All that has changed on my website is that I reorganised the structure so that instead of my pages being on the web root they are now inside http://.../newfolder/page.aspx.
For some reason all my page.aspx.cs files now have to be in app_code.
Any ideas?
Sounds like you are mixing up a Web Application Project and a Web Site.
Are you sure the files are exactly the same? Perhaps one #Page directive says CodeBehind=Page.aspx.cs and the other says CodeFile=Page.aspx.cs?
CodeBehind requires project compilation, so you cannot just drop in a new .cs file, you need to upload a new compiled DLL. CodeFile will allow dynamic compilation.
The App_Code directory is dynamically compiled (in both cases) when your app is accessed, so the Inherit directive has a valid type when you put the file there. In general, don't do this. You want the .cs file to go with the .aspx file. Use App_Code for business logic or utility classes that aren't associated with a particular page.
Finally, is this new subdirectory set up as a new app in IIS? What does the web.config file in your new directory change? Are you running the same version of ASP.NET? Check the "compilation" tag. I'm not sure what you could do there to cause this, but I'm sure you could cause some chaos.

Resources