{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Understanding ASP.NET Directory Structure
Summary
This article talks about the directory structure you see when you work with (and grow) an ASP.NET web site. Visual Studio creates some directories automatically for you and these directories have some special meaning (that means, only a certain types of files can go into these directories).
 
Table of Contents

Two Sets of Directories

About the solution file

Understanding the directory structure

Figure 1. ASP.NET folders with special meaning

Generic Directories

Solution Directory

Base Directory

Custom Directories

Directories with Special Meaning

App_Data Directory

App_Code Directory

Theme Directory

Resources Directories

App_Browsers Directory

 

Two Sets of Directories

When you create a brand new website, Visual Studio / ASP.NET uses two directories:

First one is the location directory you specified in the New Web Site dialog box. This is where you will be keeping all the files you develop for the web site; these files include the UI files (.aspx), the code files .aspx.cs (or .aspx.vb or .aspx.js), the image files (.jpg, etc.), the master files (.master), the configuration files (.config), .htm, .xml, and in the appropriate sub-directories (underneath the root directory you specified for the site) the database files and the stand-alone class files.

But then there is a second directory that you won’t notice at first. This is a departure from previous versions: the directory for the solution file.

About the solution file

Let’s talk about the solution file for a new Web Site (not a Web Project) created using Visual Studio.

The only two files that are saved here (in the solution directory) are the solution files related to the website. The main file has .sln extension, e.g. mySite.sln, which saves the project path and some other project related information.

There is also a helper / preferences file with .suo extension. By default these files are in the directory: C:\ Documents and Settings \ Administrator \ My Documents \ Visual Studio 200x \ Projects \ <your website name>. So, after you have played with creating a series of websites, you will see under the …\Projects directory a series of subdirectories holding the solution files for the websites you created.

This separation makes sense, because if you copy files to a production webserver, you don’t need the solution files over there; only your .aspx, .htm, etc. files are needed and typically you would not want to debug on the production server. In most cases, you can simply do an xcopy of the entire website directory (There are, of course, other visual/finer means of achieving the move to production server). This separation also lets you see all your projects quickly under one directory (…\Projects).

Unlike the project files of Visual Studio, these solution files don’t contain information about the individual files; it just has the name of the website directory. When you open this solution from Visual Studio, it will read the current directory and display the appropriate files in the Solution Explorer and Database Explorer. This dynamic reading of files makes it easy to add files to the solution.

You can do that either from File-Add File menu from the Visual Studio UI. Or, you can simply copy these files to the website directory and Visual Studio will show them in the Explorer windows when you open it next time.

Visual Studio is smart enough to recognize that Default.aspx.cs is the code file for Default.aspx and show them in tree-like format in the Solution Explorer. Visual Studio, of course, recognizes various extensions of files and opens them up in the right editor (within Visual Studio) and with the right document outline. It knows some special directory names as well. For example, the App_Data directory contains the database files. Various special directory names are discussed in the following section.

The only thing to remember with the separation these two sets of files is to back them up both. You will typically be paying attention to backing up the files in the website directory. But you’d also need to back up the …\Projects directory. However, since the solution file doesn’t contain a whole lot of information, recreating it is also pretty simple.

If you lost the .sln file for some reason (say, copied only the source directory on to some other computer where you plan to work on it further), just create a brand new empty website solution (from File-New Website; and select Empty Web Site template) and choose the copied directory as the location. This same approach works well if you want to rename the website directory. Simply delete the app_offline.htm file Visual Studio creates as a part of the empty website.

Understanding the directory structure

The number of files used in a web site could run into dozens for a small site, hundreds for a medium site, and, perhaps, thousands for a large site. Here we are talking about the page files, code files, image files, data files, binaries, custom control files, web service files, and so on. For a larger company, you will have multiple web sites.

Following is a listing and a brief discussion of the directories you would encounter in a typical project.

If you right-click on the web site solution (as shown in Figure 1), you will find two folder related menuitems. Choosing “New Folder” will let you create a custom folder of your choice. If you choose Add ASP.NET Folder, you can create a folder that has specific meaning to Visual Studio.

Figure 1. ASP.NET folders with special meaning

Figure 1. ASP.NET folders with special meaning

If you create a file that actually should be in one of these specialized directories, Visual Studio suggests creating that directory (automatically) if it doesn’t already exist in the project. Say, you want to create a resource file, and if an App_GlobalResources directory didn’t yet exist inside the project, Visual Studio will automatically create that for you, if you accept the message you get.

Now let’s take a look at the directories you encounter during development with Visual Studio:

Generic Directories

Solution Directory

We discussed this in the previous section. This is where the solution files are kept.

Base Directory

This is the directory we use when we create a new web site. This is the root directory that holds all the web site files and sub directory. Unless there is a good reason, you want all the directories listed below as sub-directories of this base directory.

Custom Directories

You can and should create a series of sub-directories (by choosing New Folder menuitem) to logically group your source files. For example, all your images could be in the Images sub-directory. Dowloadable files in the Dowloads sub-directory. A larger website lends itself nicely to functional categorization: for example, all the customer related files in the Customer sub-directory, the site administration files under Admin sub-directory, and so on.

Directories with Special Meaning

App_Data Directory

This sub-directory contains the database files. If you are using SQL Server Express, this is where you want to keep the .mdf files. You might have two separate databases and corresponding database files – one for storing your site-application related data (for example: customers, products, orders, partners, etc.), and another for security and user management (this would typically be named ASPNETDB.MDF). You will also have database log files in this directory (for example, aspnetdb_log.ldf, mysite_log.ldf, etc.).

It is more likely that you wouldn’t have this directory at all. Enterprise sites typically have databases running on their own servers and use sophisticated RDBMSs like SQL Server (Enterprise Edition), Oracle, DB/2, etc. In that case, you will have database connection string information in the config file, and no database files in your local directory.

App_Code Directory

Your entire website is not just a bunch of .aspx files for the user interface and .aspx.cs or .aspx.vb files for the code behind those pages. If that’s the case, you will be repeating a whole bunch of code over and over again. For example, your data access layer (the code that deals with database) could be abstracted into a set of ORM (Object-Relational Mapping) classes. There could be a whole bunch of static methods inside a utility class – these methods can be conveniently accessed from all the pages. You want to put the classes that don’t have user interface attached to them in this directory.

Theme Directory

Themes and skins are new features in ASP.NET 2.0. One fundamental difference between styles and themes is that you can apply themes at a global level after the whole web site work has been done. You can let a user customize the look of the entire website (the color scheme, images, styles, etc.) at runtime. Or, you can easily, programmatically set a different look and feel for different users, say a MemberTheme, a PartnerTheme, etc.

Themes consist of several different kinds of files: .skin, .css, images, and resources. They all should ideally reside under Theme directory (though there is some leeway for certain kinds of files).

Resources Directories

One of the primary uses of resources is to make your web application be appropriately displayed for different languages. If your web application is targeted towards speakers of different languages, the content they generate will be in their own language. For example, a Spanish user using a web-based help desk application will write the details of the issue in Spanish. However, the text of the buttons, links, and other instructions will be in the language it was developed in, most likely, English.

One very expensive way is to develop and maintain different pages for different languages: one set of pages for English, another set for Spanish, yet another set for Japanese, and so on. A better way is to use resource files that contain strings in corresponding language. At runtime ASP.NET will use the strings from the right language resource file; hence there will be only one set of aspx files for all the different languages. Needless to say, this is easier said than done (that’s why large companies have these huge localization and globalization teams). In any case, Visual Studio/ASP.NET provides great infrastructure support for this.

Resource files can also hold images, icons, audio, files, and other content. You can programmatically query for this content at runtime and include it in your pages. One advantage of this would that there won’t be a ton of image files floating around. This approach will be valuable if you want to give just one file, say custom control dll, to the end user (this file will have all the resources like images will be embedded inside).

All the resource files are stored under two directories: App_GlobalResources and App_LocalResources.

App_Browsers Directory

It is a bit ironic that the need for this directory exists. This directory contains .browser files where you define what you want to show depending on the browser.

In a perfect world, ASP.NET would some how magically make the most appropriate HTML for the browser that’s asking for your page. This works in most of the cases, but one area of problem is mobile browsers. If a person is requesting your image rich and extensive page from their cell phone or PDA, you might not want to say no to that request, but would make up and send an appropriate page for that screen size.

Take a Quick Quiz on this Article

1. Which of the following files can you expect to find in an ASP.NET theme directory?



: It’s about UI.
Question 1 of 5
Bookmark and Share This

More Articles With Similar Tags
Various subdirectories are used in a typical ASP.NET program. This is a test on the basics of various subdirectories.
Lots of different file extensions are used in ASP.NET projects. Test your knowledge on the basics these file types.
icon-aspnet-files-config.jpg
Recognizing the file types helps understand quickly the architecture of a web site that you didn’t develop (also helps with the files you didn’t explicitly create but the development environment generated). This article talks about the common and not-so common types of files you see as you develop with Visual Studio / ASP.NET.
Top Tags in Related Articles
asp.net solution directory structure file types
About  Contact  Privacy Policy  Site Map