views:

109

answers:

5

I am developing a web form using Visual Web Developer Currently I have class source files in the same directory as the web form, or in the App_Code folder. I'd like to organise the files into folders within the web form folder, but I can't find a way of adding a reference to the folders, and they don't seem to be being picked up automatically. These are files that are under constant development.

What is the asp.net/c#'s equivalent concept to c++'s #include?

Update: Note: This is a Web Site rather than a Web Application

+2  A: 

Never really thought of doing this, but i guess i would do this as follows. A folder represents a namespace. So where it says inherits="Project.PageName" in your PageName.aspx file, it should state inherits="Project.Folder.Folder.PageName". You also have to change the namespace in your PageName.aspx.designer.cs and PageName.aspx.cs files.

EDIT: For ASP.Net website simply adjust your CodeFile attribute:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Folder/Folder/Default.aspx.cs" Inherits="_Default" %>
Fabian
I have 'Inherits="_Default"', does this mean I have yet to set up a namespace?
David Sykes
No just lookup your defaultnamespace, which is most likely the name of your project. You can lookup your defaultnamespace in your project properties (right-click project in the solution explorer).
Fabian
Project properties has no mention of namespace. I think the fact that this is a web site, and not a web application, is significant.
David Sykes
Ahaa than its as simple as change the CodeFile attribute in your aspx file, updated my answer.
Fabian
Helpful, thanks, but it is the class files I am looking organise. Sorry if I wasn't clear
David Sykes
+2  A: 

I'd suggest taking these out into a separate class library project and you can then reference this DLL in your web project. You would then add a 'using' statement at the top of your web form code to include this reference.

Paddy
+2  A: 

In a C# file (foo.cs), you would use:

using MyProjectsDefaultNamespace.Folder1.Folder2

In an aspx or ascx file, you would use:

<%@ Import Namespace="MyProjectsDefaultNamespace.Folder1.Folder2" %>
Jaxidian
Where do I find MyProjectsDefaultNamespace? I'm not sure I have one set up
David Sykes
Double-click on the Properties "folder/icon" above your References folder in your Solution Explorer. It should be in the Application tab.
Jaxidian
No application tab or namespace entry. I think the fact that this is a web site, and not a web application, is significant.http://reddnet.net/code/aspnet-web-site-vs-web-application/
David Sykes
Oh, a web site. The default namespace might just be your website name. Let me spin up a web site and figure this out for you.
Jaxidian
Sorry, but I'm striking out for a web site. You may HAVE to put these into the APP_Code folder. There are MANY reasons why I try my damnedest to stay away from Web Sites in Visual Studio.
Jaxidian
I think you're right, but I can create folders in the App_Code folder, which is a great help
David Sykes
Yeah, you should DEFINITELY use a directory structure in your App_Code folder. One other thing you might want to consider is to use a Class Library (i.e. another project in your solution). This way you can have very normal class structure without having to jump through the Web Site hoops. Then you'll only have to jump through the web site hoops when consuming those objects. Or lastly, consider recreating your project into a Web Application rather than a Web Site. Those things are terrible for sooo many reasons!
Jaxidian
+1  A: 

Its not clear if you mean the codebehind .aspx.cs file or a standalone .cs class file.

You can't have .cs files loose in your main folder. It has to be in the app_code folder (or in a class library if you were doing a WAP).

Your .aspx.cs files are paired up with your .aspx file. I wouldn't recommend trying to move these files away if thats what you are trying to do.

The top level namespace that contains _Default or any code that doesnt appear to have a namespace is ASP. This is generally hidden within Visual Studio. So its true name is ASP._Default

rtpHarry
This is class files, and you're right, they won't work with the aspx file.
David Sykes
In that case I think you need to use Jaxidian's advice to make the class files available in individual pages.If you dont have a namespace wrapped around your class then you should make one - its just a case of typingnamespace MyProject.Something{ // code here}
rtpHarry
A: 

And the answer is, obtained from looking into the other answers thanks:

Organise the files into folders within the App_Code folder, they will automatically be included.

David Sykes