You are on page 1of 5

Fritz Onion’s Intro to ASP.

NET
Part 4 of 4 - Configuration and
Deployment (in C#)
Table of Contents
Fritz Onion’s Intro to ASP.NET Part 4 of 4 - Configuration and Deployment (in C#) ......... 1
Exercise 1 Storing and retrieving configuration settings ...............................................................................................2
Exercise 2 Deploying an ASP.NET application ............................................................................................................3
Fritz Onion’s Intro to ASP.NET Part 4 of 4 - Configuration and Deployment (in C#)

Fritz Onion’s Intro to ASP.NET Part 4 of 4 -


Configuration and Deployment (in C#)
After completing this lab, you will be better able to:
Objectives ƒ Store and retrieve configuration settings in web.config
ƒ Deploy ASP.NET applications to remote directories
Before working on this lab, you must have:
Prerequisites ƒ Web development experience
ƒ Completed the prior three labs in this series

Estimated Time to 30 Minutes


Complete This Lab
See Essential ASP.NET with Examples in C#, Chapter 3
For more information

Page 1 of 3
Fritz Onion’s Intro to ASP.NET Part 4 of 4 - Configuration and Deployment (in C#)

Exercise 1
Storing and retrieving configuration settings

Scenario
In this exercise, you will store the database connection string for your 'authors' page in web.config.

Tasks Detailed Steps


1. Open the starter a. Click in the virtual machine window.
project for this b. Click the Administrator icon.
exercise
c. Log on with a password of password.
d. Click Start | All Programs | Microsoft Visual Studio .NET 2003 | Microsoft
Visual Studio .NET 2003.
e. Select File | Open Solution.
f. Navigate to the file
c:\labs\AspDotNetIntroLab4\before\AspDotNetIntroLab4.sln and click Open.
2. Add a new a. In the Solution Explorer double-click the Web.config file.
appSettings element b. Add a new appSettings element just under the opening <configuration> element
and store a 'dsn' key
<appSettings>
in web.config
</appSettings>
c. Add a new add element with the key set to dsn and the value set to the dsn for
your local pubs database
<add key="dsn"
value="server=.;trusted_connection=yes;database=pubs" />
3. Modify your data a. In the Solution Explorer right-click the Default.aspx page and select View Code
access code to to open the code-behind file.
retrieve the database b. Add a using directive to the top of the file for System.Configuration by typing:
connection string
using System.Configuration;
from web.config
c. At the top of your Page_Load handler, extract the value for the dsn key from
web.config using the ConfigurationSettings class
string dsn = ConfigurationSettings.AppSettings["dsn"];
d. Modify your SqlConnection and SqlDataAdapter constructor invocations to use
the dsn you retrieved from Web.config. The affected code should then look like
this:
using (SqlConnection conn = new SqlConnection(dsn))
…/…
SqlDataAdapter da = new SqlDataAdapter("SELECT
au_fname + ' ' + au_lname AS name, au_id FROM authors",
dsn);
4. Compile and test! a. In the Solution Explorer right-click Default.aspx and select Set As Start Page.
b. Click Debug | Start Without Debugging.
c. Verify that the application runs successfully and close the Internet Explorer
window.

Page 2 of 3
Fritz Onion’s Intro to ASP.NET Part 4 of 4 - Configuration and Deployment (in C#)

Exercise 2
Deploying an ASP.NET application

Scenario
In this exercise, you will try deploying your 'authors' application to a new virtual directory, making careful note of
which files must be copied and which are unnecessary.

Tasks Detailed Steps


1. Your current project a. Navigate to the c: drive and create a new physical directory called authors
is housed in a (c:\authors).
directory with all of b. Click Start | Administrative Tools | Internet Information Services.
the source code files,
c. Expand CLIENT1 (local computer) | Web Sites | Default Web Site.
binaries, project files,
etc. Try deploying d. Right-click on the Default Web Site and select New | Virtual Directory….
only the necessary e. Click Next.
files to a new virtual f. On the Virtual Directory Alias page, in the Alias field, type authors and click
directory Next.
g. On the WebSite Content Directory page click Browse and navigate to c:\authors
and click OK.
h. Complete the wizard with default settings and click Finish.
i. Close Internet Information Services.
2. Prepare your project a. Switch to Visual Studio .NET.
for deployment b. On the toolbar, in the Solution Configurations drop-down box, select Release.
c. Switch to Web.config and locate the <compilation...> element.
d. Change the debug=”true” attribute of this element to “false”.
3. Copy all necessary a. Navigate to c:\labs\AspDotNetIntroLab4\before and copy all the following files
files to your new to c:\authors:
directory *.aspx.
*.asax.
*.ascx
Web.config.
/bin.
4. Try running your a. Click Start | Internet Explorer and navigate to
newly deployed site! http://localhost/authors/default.aspx to access the newly deployed site.

Page 3 of 3

You might also like