
Professional Visual Studio 2005 (2006) [eng]
.pdf
Chapter 41
Because this process involves sending an e-mail, you need to set up a couple of properties and settings to make it work. First, e-mail messages must have a From e-mail address set. You’ll find this in the Mail Definition section of properties for the PasswordRecovery component; along with From you can also set the Subject line and specify the importance of the e-mail that will be sent.
The other change you’ll need to make is to add a new section of settings in the Web.config file. Because sending e-mail requires a default SMTP server, you’ll need to save this in the Web.config file; the SMTP information is kept in the system.net settings area. The following listing provides an example of the minimum code you’ll need to add to the Web.config file:
<system.net>
<mailSettings>
<smtp deliveryMethod=”network”> <network
host=”mail.mymailserver.com”
port=”25”
defaultCredentials=”true”
/>
</smtp>
</mailSettings>
</system.net>
The system.net node resides at the same level as system.web inside configuration.
The last component in the Login group on the Toolbox is the LoginView object. LoginView enables you to create whole sections on your web page that are only visible under certain conditions related to who is (or isn’t) logged in. By default, you have two views: the AnonymousTemplate, which is used when no user is logged in, and the LoggedInTemplate, used when any user is logged in. Both templates have an editable area that is initially completely empty.
However, because you can define specialized roles and assign users to these roles, you can also create templates for each role you have defined in your site (see Figure 41-17). The Edit RoleGroups command on the smart tag Tasks list associated with LoginView displays the typical collection editor and enables you to build role groups that can contain one or multiple roles. When the site detects that the user logs in with a certain role, the display area of the LoginView component will be populated with that particular template’s content.
Figure 41-17
See the “Site Administration” section later in this chapter for more information about roles.
566

Creating Web Applications
What’s amazing about all of these controls is that with only a couple of manual property changes and a few extra entries in the Web.config file you can build a complete user authentication system into your web application. In fact, as you’ll see in the “Site Administration” section later in this chapter, you can edit all these settings without needing to edit the Web.config file directly. Now that’s efficient coding!
Data Controls
Data controls were introduced to Microsoft web developers with the first version of Visual Studio .NET, but have evolved to be even more powerful with Visual Studio 2005. Each data control has a smart tag Tasks list associated with it that enables you to edit the individual templates for each part of the displayable area. For example, the DataList has seven templates in all, which can be individually customized (see Figure 41-18).
Figure 41-18
The DataList also has a property builder that gives you total control over the formatting of individual items. Figure 41-19 shows the Properties dialog with the Format page open, with all possible element types displayed. However, you can turn off unneeded components in the General page, such as hiding the header or footer, and specify whether to use the default vertical orientation for the data or to flip it to horizontal data viewing.
Figure 41-19
567

Chapter 41
Master/Detail Content Pages
A new feature of web development in Visual Studio 2005 is the capability to create master pages that define sections that can be customized. This enables you to create a single page design that contains the common elements that should be shared across your entire site, specify areas that can house individualized content, and inherit it for each of the pages on the site.
To add a master page to your web application project, use the Add New Item command from the Website menu or from the context menu in the Solution Explorer. This displays the Add New Item dialog, which contains a large number of item templates that can be added to a web application (see Figure 41-20). You’ll notice that besides Web Forms (.aspx) pages and Web User Controls, you can also add plain HTML files, XML files and schema, and other web-related file types. To add a master page, select the Master Page template, choose a name for the file, and click OK.
When a master page is added to your web site, it starts out empty except for a single ContentPlaceHolder component. This is where the detail information can be placed for each individual page. You can create the master page in the same way as any other web form page, complete with ASP.NET and HTML elements, CSS style sheets, and theming.
Figure 41-20
If your design requires more than one area for detail information, you’ll need to switch to Source view and add the following tags where you need the additional area:
<asp:contentplaceholder id=”aUniqueId” runat=”server”>
</asp:contentplaceholder>
Once the design of your master page has been finalized, you can use it for the detail pages by adding new web forms to your project. Figure 41-20 shows an extra option of Select Master Page, which is grayed out because in the current context the developer is creating a master page item. However, this checkbox becomes available when you select the Web Form template.
Visual Studio 2005 will remember the state of this checkbox when you add additional web forms.
568

Creating Web Applications
When you check the Select Master Page option, you’ll be presented with an additional dialog listing the master pages currently defined in your web application. Select the master page to be applied to the detail page and click OK. The new web form page that is added to the project will inherit the design and layout of its master page. However, you’ll be able to edit only the areas defined as ContentPlaceHolders; any other area of the page will be locked. The only place you can edit those areas is in the master page itself.
Finalizing and Deployment
Once you have your site designed and working in a development environment, you’re going to want to finalize the site settings so that it works in a way the user will expect. The Start Options page of the site properties contains settings that control how the site will be started. To display the Start Options, either use the Website Start Options menu command or right-click on the web project you want to configure and select Property Pages from the context menu.
Either way, you’ll gain access to the Start Options section of the project properties, as displayed in Figure 41-21. The default behavior specifies that running the site will use the current page as its startup page. This is actually really handy when developing sites. Previously you needed to run the site and always go to the default page as defined in the Web.config file. Then you would navigate to the page you were actively developing, which could be inefficient depending on the complexity of your site.
Figure 41-21
With Visual Studio 2005’s default start behavior, you can go directly to the page you’re currently editing, which enables you to be more efficient in testing and debugging your web applications.
However, when deploying the site, you’re going to want to set a specific page or external application to start the site so users always have a consistent viewing experience when they access it. You can specify a starting URL or a specific page that is relative to the root folder of the application. Alternatively, you can specify an application that will control how your web application starts, and even define a different web server to run the project.
569

Chapter 41
At the bottom of the Start Options page is a section where you can specify what debuggers are to be activated. Note that while the ASP.NET debugger is turned on by default, you can only debug your application if the Web.config file is edited to allow debugging. Unlike previous versions of Visual Studio .NET, the default Web.config file has debugging disabled so you don’t accidentally deploy an application to the web with debugging on.
Deploying the Site
If the site is already on a remote server, whether it was an HTTP, FTP, or FrontPage server, you don’t need to do anything else for it to be up and running for your users. However, if you created the web application in the local file system, you’ll need to deploy it to the final destination.
To copy your web application to the deployment destination, use the Website Copy Web Site menu command. This will display the Copy Web interface in the main workspace area (see Figure 41-22).
Figure 41-22
If you have previously copied the site to another destination, the Connections textbox will be populated with the previous location. Click the Connect button to bring up the Connections dialog, which is similar to the Browse dialog displayed when you’re first creating a web solution, with options for local file system, HTTP, and FTP locations.
Once you’ve specified the required destination location, Visual Studio connects to the remote site and lists the files that are present. You can then select the individual files or folders you want to copy over to the site and click either the Copy or Synchronize buttons that are situated between the Source Web site and Remote Web site views.
570

Creating Web Applications
Synchronize checks both locations to ensure they have the same version of the selected file. If the file versions are different, whichever location is deemed to be the most recent is used as the source and is copied to the other location.
Site Administration
While running your web application with default behavior will work in most situations, sometimes you’ll need to manage the application settings beyond simply setting the properties of components and page items. The Web Site Administration Tool provides you with a web-based configuration application that enables you to define various security-related settings, including defining users and roles, as well as applicationwide settings that can come in handy, such as a default error page and the SMTP mail settings that were mentioned earlier in the discussion about the PasswordRecovery component.
To start the Administration Tool, use the Website ASP.NET Configuration menu command. If necessary, Visual Studio 2005 instantiates a temporary web server on a unique port and starts the tool itself, pointing it to the application you’re currently administering.
You can determine whether the web server is active by looking in the notification area of your taskbar and finding the Development Server icon connected to the port Visual Studio 2005 allocated when it was started up. You can stop an active web server by right-clicking its icon in the notification area and selecting Show Details. When the server information is displayed (see Figure 41-23), click the Stop button to stop the specific instance of the development web server.
Figure 41-23
Note that this won’t affect any other development servers that are currently running.
When the Administration Tool is displayed in your web browser, it will show the application name, accompanied by the current Windows-based authenticated user. There are three main sections to the tool: security to create and maintain users, roles, and authentication; application configuration to control application specific key-value pairs, SMTP settings, and debug configurations; and provider configuration to control the way the user administration data is stored for the site.
571

Chapter 41
Security
The security section of the tool provides you with a summary of the users and roles defined in the site, and the authentication mode. You can change individual settings from this summary page by clicking on their associated link, or use the Security Setup Wizard to step through each section of the security settings in turn.
The authentication mode is controlled by the access method page (shown in the wizard in Figure 41-24). If you choose From the Internet, the tool sets the authentication mode to Forms, while the From a Local Area Network option results in an authentication mode of Windows.
The most useful part of this tool is the capability to add and edit roles. In the wizard you’ll first need to enable role management by checking the Enable Roles for this Web Site option. Once roles are active, you can define them either through the wizard or from the summary page. Each role is defined by a single string value, and it’s up to you to control how that role will be used in your web application (with the exception of access rules, which are discussed in a moment).
The next step of the wizard is to create user accounts. The information on this page is a replication of the CreateUserAccount component, and enables you to create an initial user who can serve as administrator for your web site.
Figure 41-24
572

Creating Web Applications
The access rules page (shown in Figure 41-25) enables you to restrict access to certain parts of your site to a specific role, a specific user, or only when any user is logged in. As Figure 41-25 shows, by default there is a single rule (which is actually implicitly defined and inherited from the server) that defines full access to the entire site for all users.
Web site processing will look at the rules in the order they are defined, stopping at the first rule that applies to the particular context. For example, if you defined first a rule that allowed access to the App_Data folder for anyone belonging to the Administrator role and then a subsequent rule that denied access to the same folder for all users, it would effectively block access to the App_Data folder to all users who do not belong to the Administrator role.
Once you’ve got users, roles, and rules defined in your site, you can then start applying the access by clicking the Manage Users link from the summary security page. This will present you with a list of all users defined in the system. Click on the Edit User or Edit Roles links to specify the roles to which each user belongs.
This information can be used to customize the content in your web pages with the LoginView component discussed earlier in this chapter.
Figure 41-25
573

Chapter 41
Application Settings
The application section of the Web Site Administration Tool enables you to define and edit applicationspecific settings in the form of key-value pairs, as well as configure the SMTP e-mail settings, including the default SMTP mail server and sender e-mail address.
You can also specify what level of debugging you want to perform on the application, and customize the tracing information being kept as you run the application.
ASP.NET 2.0 Configuration Settings
An alternative to using the Web Site Administration Tool is to edit the configuration settings directly within Internet Information Services, located in the Administrative Tools section of the Control Panel. When ASP.NET 2.0 is installed on your machine, you’ll find that each web site (including virtual directories) will have a new ASP.NET tab in the site’s Properties pages.
If the site is defined as using ASP.NET 2.0, you can then edit the configuration settings directly without having to get into the Web.config file of the application. The Configuration Settings dialog displayed in Figure 41-26 reveals the default SQL Server Express database that is used for user authentication settings that were discussed earlier. You can easily add individual application setting key-value pairs as well as other configuration options, such as authentication type and error page redirections, that used to be available only if you edited Web.config directly.
To be fair, the Web Site Administration Tool and the Configuration Settings dialog in IIS serve slightly different purposes, so judicious use of both will help maintain your web application without you having to resort to editing XML-based configuration files.
574 |
Figure 41-26 |

Creating Web Applications
Summar y
In this chapter you learned how to create web applications of various types, including web services. The discussion included the following highlights:
How Visual Studio 2005 works in the absence of IIS through the use of temporary virtual web servers
Tools to make administration of the site straightforward
Details on some of the new web controls and components
How to theme and style components
Deploying your solution to the production site
Using master pages to apply common components across your site
Of course, there’s more to web development than this introduction. In the next chapter you’ll see some of the advanced techniques you can use to maintain efficiency on your development projects without sacrificing power. Specifically, you’ll look at web parts and templates that help you compartmentalize your web-based development components. In addition, the chapter offers a brief introduction to how you can create custom controls for the web.
575