I use VS2008 to create a Asp.net web application. Then I create a App_Code new folder. After that I add a new item, Class1.cs, under App_Code.
When I come back to default.aspx.cs and type Class1 within Page_Load, I found it is not recognized. If I compile, compiler error says "Class1" could not be found.
If you are using the "web application project" instead of "web site", then the code does not get dynamically compiled. You would be better off adding these classes a class library project and adding the reference to your web application project
However if you are using website , the class in the App_code will get compiled dynamically
Jeev
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you get the answer to your question, please mark it as the answer.
I did use "web application project". You answer explains.
I tried this. Add new Class.cs. Build project. Then reference Class1 from default.aspx.cs. It still deosn't work. This leads to another question then. Does this mean, in "web application project", I'm not able to add a new Class file to define new classes?
Did you used in VS2005? If so, there are several suggestions.
1. You can put the new class file into any folder EXCEPT App_Code folder. It means that you can't directly access the class in the App_Code folder and you can put the class file in other folders.
2. For example, the new class file "Class1" in the folder "ppp".
namespace WebApplication1.ppp
{
public class Class1
{
}
}
You can call the "Class1" with WebApplication1.ppp.Class1 in the default.aspx.cs
But in VS2008, this issue has been resolved and every thing is ok.
Hope it helps.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Marked as answer by Vince Xu - MSFT on Feb 06, 2008 05:09 AM
I am using .Net3.5 and Asp.Net2008. I have created a web application and added a folder "App_Code" and a class file in "App_Code" folder. But I am not able to get the reference/intellisence for the class in any WebForm.aspx.cs file. Do anyone help?
using
System;
using
System.Linq;
using System.Xml.Linq;namespace WebApplication1.App_Code
If you use a "web application" project you'll want to add the class outside of the \app_code directory. When you do this it will be compiled with the rest of your application and you'll have intellisense against it.
If you use a "web site" project you can pull the class in \app_code and get intellisense.
Hope this helps,
Scott"]
This means that we cannot add \app_code and class files in \app_code folder when we are using "web application", only we can add \app_code folder in "web site".
In Visual Studio Professional 2008, you can still create the
App_Code folder by right-clicking on the Web Project and selecting
Add, then Add Folder. Rename the new folder to App_Code.
Contrary to some recommendations on the Web that you should not use the App_Code folder because you cannot place common Web UI codes or classes in this folder, you can do so by setting
Build Action of each class to
Compile. Suppose this step is not done, the classes defined in this folder will not be visible to your other codes. This explains why people recommended against the use of App_Code folder in VS 2008.
Suppose you have a class called WebCommon.cs in this folder. All you have to do is to right-click on this file and select
Properties. A properties window will appear. Set
Build Action to
Compile. Viola! The class can be accessed exactly like what you have seen in VS 2005.
taowang
Member
8 Points
37 Posts
why my new Class is not visible in default.aspx.cs?
Jan 31, 2008 10:44 PM|LINK
Hi,
I use VS2008 to create a Asp.net web application. Then I create a App_Code new folder. After that I add a new item, Class1.cs, under App_Code.
When I come back to default.aspx.cs and type Class1 within Page_Load, I found it is not recognized. If I compile, compiler error says "Class1" could not be found.
Any idea?
Thx
Tao
Jeev
All-Star
24182 Points
3719 Posts
Re: why my new Class is not visible in default.aspx.cs?
Jan 31, 2008 11:05 PM|LINK
If you are using the "web application project" instead of "web site", then the code does not get dynamically compiled. You would be better off adding these classes a class library project and adding the reference to your web application project
However if you are using website , the class in the App_code will get compiled dynamically
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you get the answer to your question, please mark it as the answer.
dinesh_sp
Contributor
2272 Points
413 Posts
Re: why my new Class is not visible in default.aspx.cs?
Jan 31, 2008 11:07 PM|LINK
Hi Tao, Have you made it public and secondly have you added some properties or methods to it???
taowang
Member
8 Points
37 Posts
Re: why my new Class is not visible in default.aspx.cs?
Feb 01, 2008 03:23 AM|LINK
Jeev,
I did use "web application project". You answer explains.
I tried this. Add new Class.cs. Build project. Then reference Class1 from default.aspx.cs. It still deosn't work. This leads to another question then. Does this mean, in "web application project", I'm not able to add a new Class file to define new classes?
Class1 is public with no property.
Thx
Tao
Vince Xu - M...
All-Star
80367 Points
6801 Posts
Re: why my new Class is not visible in default.aspx.cs?
Feb 04, 2008 04:10 AM|LINK
Hi,
Did you used in VS2005? If so, there are several suggestions.
1. You can put the new class file into any folder EXCEPT App_Code folder. It means that you can't directly access the class in the App_Code folder and you can put the class file in other folders.
2. For example, the new class file "Class1" in the folder "ppp".
namespace WebApplication1.ppp
{
public class Class1
{
}
}
You can call the "Class1" with WebApplication1.ppp.Class1 in the default.aspx.cs
But in VS2008, this issue has been resolved and every thing is ok.
Hope it helps.
gyan_flip
Member
129 Points
77 Posts
Re: why my new Class is not visible in default.aspx.cs?
Apr 25, 2008 11:13 AM|LINK
Below:
ASP.NET 3.5 .net 3.5
gyan_flip
Member
129 Points
77 Posts
Not able to access class of App_Code folder?
Apr 25, 2008 11:15 AM|LINK
I am using .Net3.5 and Asp.Net2008. I have created a web application and added a folder "App_Code" and a class file in "App_Code" folder. But I am not able to get the reference/intellisence for the class in any WebForm.aspx.cs file. Do anyone help?
using
System;using
System.Linq; using System.Xml.Linq;namespace WebApplication1.App_Code{
public class Employee{
public string Name { get; set; }}
}
Regards,
Gyan
ASP.NET 3.5 .net 3.5
gyan_flip
Member
129 Points
77 Posts
Re: Not able to access class of App_Code folder?
Apr 29, 2008 08:38 AM|LINK
Reply From Scott:
[" Hi Gyan,
If you use a "web application" project you'll want to add the class outside of the \app_code directory. When you do this it will be compiled with the rest of your application and you'll have intellisense against it.
If you use a "web site" project you can pull the class in \app_code and get intellisense.
Hope this helps,
Scott "]
This means that we cannot add \app_code and class files in \app_code folder when we are using "web application", only we can add \app_code folder in "web site".
ASP.NET 3.5
dabhi_mayur
Member
163 Points
89 Posts
Re: why my new Class is not visible in default.aspx.cs?
May 16, 2010 03:58 PM|LINK
App_Code Folder in ASP .NET 3.5
In Visual Studio Professional 2008, you can still create the App_Code folder by right-clicking on the Web Project and selecting Add, then Add Folder. Rename the new folder to App_Code.

Contrary to some recommendations on the Web that you should not use the App_Code folder because you cannot place common Web UI codes or classes in this folder, you can do so by setting Build Action of each class to Compile. Suppose this step is not done, the classes defined in this folder will not be visible to your other codes. This explains why people recommended against the use of App_Code folder in VS 2008.
Suppose you have a class called WebCommon.cs in this folder. All you have to do is to right-click on this file and select Properties. A properties window will appear. Set Build Action to Compile. Viola! The class can be accessed exactly like what you have seen in VS 2005.
skamin
Member
28 Points
10 Posts
Re: why my new Class is not visible in default.aspx.cs?
Jun 24, 2010 01:27 PM|LINK
Thanks dabhi ... this is a real solution to an annoying anomaly between web app projects and website projects .. good man.