Reputation: 13420
I have a couple of classes that I have created for my web project and I am going to store them in the App_Code folder. I don't want to store all the classes in the top level (no namespace) but want to organize them into namespaces. So far this does not seem possible because when I use the "using XXXX;" namespace statement it still can't find the right class in my code behind files. What could I be doing incorrectly?
EDIT:
So for example I have
namespace foo
{
public class bar
{
}
}
Then in my code behind page for default.aspx I have
.
.
.
using foo
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
bar NewBar = new bar();
}
}
The above DOES NOT work for me I have to use
foo.bar NewBar = new foo.bar();
Does anyone have any idea what I am doing wrong?
EDIT: Okay, so I have installed Visual Studio 2005 SP 1 and tried to convert it to a Web application and not a web site. STILL no luck.
Upvotes: 3
Views: 5384
Reputation: 109
Right click on "class" and select-> "property" change the "property" to-> compile and on th .aspx "write the namespace name"
using namespace name of the class;
It works fine.
Upvotes: 0
Reputation: 13420
I have figured it out. I am really stupid. Sorry for the worry. It was because I had the namespace and the class name the same so it could not differentiate and needed the fully qualified name. As soon as I changed the class name everything worked as expected.
Upvotes: 1
Reputation: 5924
I just tried your exact code sample in VS2005 and it works as expected with the using directive, no need to build the site first or anything.
That tells me that there's something else wrong with your project and/or namespaces. Will it build if you right-click and choose "Build Web Site"?
When you say it DOES NOT WORK, in what way exactly does it not work? Does the site not build? No intellisense completion? Error message when you browse to the page?
Upvotes: 0
Reputation: 29956
The easiest solution would be to put the classes in a Class Library and then reference it from your web site project.
Upvotes: 3
Reputation: 1362
(I'm guessing here) Try opening the file's properties and setting the build action to "compile".
Upvotes: 11
Reputation: 2977
You are using a Web Site Project, rather than a Web Application Project. Web Site Projects compile all the .cs files (including those in App_Code) on the fly, so that you can update them on the web server, without recompiling them. They are compiled into temporary dlls, which, under normal circumstances, will not be visible to you (ie, nothing in the bin). You have very little control of namespaces when writing Web Site Projects, as you have discovered. If you wish better structured code and more control over namespaces, then convert it to a Web Application Project. Your server-side code will be compiled into a dll and placed in the bin folder.
Upvotes: 1
Reputation: 1179
Have you tried building the project first of all and then try "using" them ?
Upvotes: 0