Section 2.2. Projects and Solutions

2.2. Projects and Solutions

A typical .NET web application consists of many items: content files (such as .aspx files), source files (such as .cs files), assemblies (such as .exe and .dll files) and assembly information files, data sources (such as .mdb files), references, and icons, as well as miscellaneous other files and folders. VS2005 organizes these items into a folder that represents the web site. All the files that make up the web site are contained in a solution . When you create a new web site, VS2005 automatically creates the solution and displays it in the Solution Explorer. Additional projects (described below) can also be added to the web site.

In addition to web sites, VS2005 can create projects . These projects can be added to the solution or placed in their own solution. Many types of projects can be built in VS2005, including among others:

  • Windows Application

  • Windows Service

  • Windows Control Libray

  • Web Control Library

  • Class Library

  • Pocket PC Application, Class Library, Control Library, or Empty Project

  • SmartPhone Application, Class Library, or Empty Project

  • Windows CE Application, Class Library, Control Library, or Empty Project

  • SQL Server Project

  • Empty Project

Notable in its absence from the above list are Web Applications. Web applications are not contained within projects, just solutions and, as you saw above, you open new web sites directly, using File New Web Site... rather than File New Project. You can, however, add a project to a web application solution. You will see this demonstrated in Chapter 19 when you learn how to create setup projects for deploying your finished web site.

2.2.1. Solutions

Solutions typically contain one or more projects and /or web sites. They may contain other, independent items as well. These independent solution items , such as business case presentations, specification documents, or timelines , are not specific to any particular project but apply, or scope , to the entire solution. The solution items are not an integral part of the application because they can be removed without changing the compiled output. They display in Solution Explorer (described later in this chapter) in a Solution Items folder and can be managed with source control.

Miscellaneous files are independent of the solution or project, but they may be useful to have handy. They are not included in any build or compile but will display in the Solution Explorer and may be edited from there. Typical miscellaneous files include project notes, database schemas , or sample code files. To display the Miscellaneous Files folder as part of the solution, go to Tools Options Environment Documents, and check the checkbox for S how Miscellaneous files in Solution Explorer.

You can have a solution that does not contain any projects but contains only solution items or miscellaneous files, which can be edited using VS2005. This might be handy just to take advantage of the editing, organizational, and (with additional software such as Visual Source Safe) source control capabilities.

Solutions are defined by a solution file , created by VS2005 and named for the solution with a .sln extension. The .sln file contains a list of the projects that comprise the solution, the location of any solution-scoped items, and any solution-scoped build configurations. VS2005 also creates a .suo file with the same name as the .sln file (e.g., mySolution.sln and mySolution.suo ). The .suo file contains data used to customize the IDE on a per- user and per-solution basis.

In previous versions of Visual Studio, the .suo file was maintained only on a per-solution and not a per-developer basis.


The solution file is placed in the Visual Studio projects location. By default, it will look like this (with your user name substituted):

 c:\Documents and Settings\dhurwitz\My Documents\Visual Studio\Projects 

You can change it to something a little easier to navigate, such as this:

 c:\vsProjects 

Go to Tools Options Projects and Solutions General and change the default Visual Studio projects location.

You can open a solution in VS2005 by double-clicking the .sln file in Windows Explorer. Even if the .sln file is missing, you can still open a project in VS2005. A new .sln file will be created when you save.

Let's be clear here: there is no project file, but there is a project folder. There are no solution folders, but there is a solution file that lives in a project folder. A solution file may reference multiple projects, including projects from other project folders. Okay, maybe "clear" wasn't the right word.

Furthermore, the Solution Explorer in VS2005 (described below) displays projects as though they are contained within solutions even though the physical directory structure does not support this interpretation.

This can be a bit confusing, but it all comes together and works well enough once you get used to it. It makes it much easier to work with projects since they can easily be opened up wherever they happen to be located. Project files from previous versions of Visual Studio .NET are still supported.


2.2.2. Projects and Files

A project contains content files , source files, and other files such as data sources and graphics. Typically, the contents of a project are compiled into an assembly, such as an executable file ( .exe ) or a dynamic link library (DLL) file, which can be identified by its .dll extension.

Most of the content of a web page or user control (user controls are described in Chapter 14) consists of server control declarations and HTML. This content, along with any necessary directives (directives are described in Chapter 6) and script comprise the content file for the page or user control. Content files for web pages have an extension of .aspx and for user controls have an extension of .ascx . There are other types of content files in ASP.NET, listed in Table 2-1.

Table 2-1. Content file types

File type

Extension

Page

.aspx

User Control

.ascx

Web Service

.asmx

Master Page

.master


The script contained within content files can be contained in script blocks delimited by <script> tags or in-line with HTML delimited by <% %> tags. The script can be run client-side or server-side. If the script is written in JavaScript or VBScript, as indicated by a language attribute in the script block, it is sent to the browser and run client-side. If the script block contains a runat ="server" attribute, then the code it contains, written in the .Net language specified in the Language attribute of the Page directive, is compiled and run server-side.

ASP.NET supports code separation, where the server-side source code, (the C# or VS2005) is contained in a code-behind file separate from the content file. The code behind file typically has an extension indicating the programming language, such as .cs.

Server-side script and code contained in code-behind files are all compiled into a single class. Code separation and code-behind files are discussed in detail in Chapter 6.

Code-behind is the default coding model used by VS2005. When a new web site is created, VS2005 automatically creates two files: the content file, with a default name, such as Default.aspx , and a code-behind file with a matching name, such as Default.aspx.cs ( assuming you are using C# as your programming language). If you change the name of the content file (highly recommended), the code-behind file will automatically assume the new name.

To see this at work, right-click on Default.aspx in the Solution Explorer and choose View Code. The file Default.aspx.cs will open with a partial class definition of the class used to support your page (_Default) that inherits from the System.Web.UI.Page class. To get you started, a skeleton Page_Load event handler is provided (this event handler is explained in Chapter 6.

For more on partial classes, see Programming C# or Visual C# 2005: A Developer's Notebook , both by Jesse Liberty (O'Reilly).


2.2.3. Templates

When you create a new project by clicking the New Project... link on the Start Page (shown in Figure 2-1) or File New Project..., you will get the New Project dialog box, as shown in Figure 2-6.

Figure 2-6. New Project dialog box when nothing is currently open

As described previously, web applications are not created by creating a new project but by creating a new web site.


To create a new project, you select a project type and a template. Various templates exist for each project type. For example, the templates for Visual C# Projects, shown in Figure 2-6, are different from the templates available in the Other Project Types/Setup and Deployment section. You can create an empty solution, ready to receive whatever items you want to add.

The template controls what items will be created automatically and included in the project, as well as default project settings. For example, if your project is a C# Class Library, a language-specific .cs file will be created as part of the project. If the project is a VS2005 project, the corresponding .vb file will be created instead. If a different template were selected, an entirely different set of files would be created.

2.2.4. Project Names

Project names may consist of any standard ASCII characters except for the following:

  • Pound (#)

  • Percent (%)

  • Ampersand (&)

  • Asterisk (*)

  • Vertical bar ()

  • Backslash (\)

  • Colon (:)

  • Double quotation mark (")

  • Less than (<)

  • Greater than (>)

  • Question mark (?)

  • Forward slash (/)

  • Leading or trailing spaces

  • Windows or DOS keywords, such as "nul," "aux," "con," "com1," and "lpt1"



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net