CHAPTER 10 ASP.NET 231 Table 10-2. Continued Short c ut Note s script Client script block scriptr Server script block scriptref Client script reference select span style table ul xhtml10f XHTML 1 frameset declaration xhtml10s XHTML 1.0 Strict document declaration xhtml10t XHTML 1.0 Transitional document declaration xhtml11 XHTML 1.1 doctype Deployment I think one of the biggest and most valuable changes in ASP.NET 4.0 is the new deployment functionality. VS2010 makes it easier than ever before deploy your ASP.NET application. VS2010 allows you to • Perform transformations on Web.config for different build configurations. • Create web packages (zip files that contain your application, database, and settings in a single file). • Install your application and all its settings with just one click (one-click publishing). Automating your deployment processes is a smart move, because it reduces mistakes, saves you time, and creates a repeatable and self-documented process. Web.config Transformation Many developers use Web.config file to hold application specific settings, such as database connection strings. The trouble with Web.config is that when you deploy your applications, you normally have to change these settings. VS2010 offers the ability to create a build configuration that allows you to modify the contents of the Web.config file for different build configurations. Note you can also use this feature on Web.config files in sub folders. VS2010 actually uses this functionality already when you switch between debug/release mode, let’s take a look at this now. CHAPTER 10 ASP.NET 232 1. Create a new ASP.NET web application called Chapter10.WebConfigTransformation. 2. Click the Show All Files option in Solution Explorer. 3. Expand Web.config. Note how Web.config has two files nested beneath it: Web.Debug.config and Web.Release.config . 4. Open Web.Release.config. It will have code similar to the following (shortened for brevity): <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration> This is a simple transformation that is used to remove debug attributes when the application is built in release mode. Creating a New Deployment Configuration I will now walk you through creating your own transformation. Let’s imagine we have a server allocated for user acceptance testing (UAT) and we need to change an individual setting when our application is moved from our development machine to the user-acceptance machine. 1. The first thing we need to do is create a new build configuration, so go to the build configuration manager. On the main menu, go to BuildConfiguration Manager. 2. Click the drop-down menu labeled Active solution configuration and select <New>. 3. Enter the name UAT. 4. Select the “Copy settings from Release” drop-down menu option. 5. Make sure the “Create new project configurations” checkbox is ticked and click OK. 6. Close the Configuration window. 7. Right-click on Web.config in Solution Explorer and select the Add Config Transforms option. VisualStudio will create a new Web.UAT.config file. 8. Open Web.config and add a new appsetting key called RunMode: <?xml version="1.0"?> <appSettings> <add key="RunMode" value="Default" /> </appSettings> 5. Now open Web.UAC.config and replace the code with the following: <?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="RunMode" xdt:Transform="Replace" xdt:Locator="Match(key)" value="User acceptance testing" /> </appSettings> CHAPTER 10 ASP.NET 233 <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration> 6. If you want to run your app to see if the changes have occurred, open Default.aspx.cs and add the following code to Page_Load: Response.Write(System.Configuration.ConfigurationManager.AppSettings.Get("RunMode")); If you run your project now you will not see the results of this transformation. This is because transformations are part of your build process and will only occur when you publish your application or use the MSBuild utility with certain options. This does seem a bit of shame, as it would have been nice to easily test transformations by changing the build mode. The easiest way to see the transformations is to publish the project. 1. Right-click on the project and select Publish. 2. Change the Publish Method in the drop-down menu to File System, enter a path where the project should be built, and click the Publish button. If you now open the Web.config in your published application you will find the RunMode setting has been changed. Transforming Web.config from the Command Line You can also transform Web.config from the command line. 1. Open VisualStudio command prompt. 2. Change the path to your current project directory and enter the following command: MSBuild Chapter10.WebConfigTransformation.csproj /t:TransformWebConfig /p:Configuration=UAC 3. If you now take a look at the ~/obj/UAC/TransformWebConfig folder of your application, you will find the default Web.config file has had the transformation applied to it. Web.config Transformation Options I have just demonstrated a very simple replace, but VS2010 allows you to carry out much more complex changes. VS2010 offers 3 options to locate the item you want to change (full details at http://msdn. microsoft.com/en-us/library/dd465326%28VS.100%29.aspx): • Match on a node attribute value xdt:Transform="Replace" xdt:Locator="Match(name)" • Match on XPath expression xdt:Locator="XPath(//system.web)" • Match on value xdt:Locator="Condition(@name=’MyDb’)" CHAPTER 10 ASP.NET 234 The following actions can be performed on Web.config settings: • Replacement xdt:Transform="Replace" • Insertion xdt:Transform="Insert" xdt:Transform="InsertAfter(/configuration/system.web/authorization/ allow[@roles=Authors])” xdt:Transform="InsertBefore(/configuration/system.web/authorization/ deny[@users='Alex'])" • Deletion xdt:Transform="RemoveAll" • Removal xdt:Transform="RemoveAttributes(debug,batch)" • Setting individual attributes xdt:Transform="SetAttributes(batch)" Web Packages Deploying an ASP.NET application is often more complex than simply copying files to a remote server. Applications often need specific IIS settings, GAC components, COM DLLs, and so on. VS2010 allows you to wrap your application up in a package that contains all this functionality. The following can be included in a web package: • The application's pages, controls, images, CSS, media, and so on (duh!) • GAC assemblies • COM components • IIS settings • SQL databases • Security certificates Web packages are created by the MsDeploy tool and contain your application’s content and additional information that describes its setup requirements. Once you have created your web package, it can be deployed in three main ways: • Manually, by using IIS’s management features and selecting Import Application • By entering the following in the command line MSBuild "MyProjectname.csproj" /T:Package /P:Configuration=UAC • Using the deployment API CHAPTER 10 ASP.NET 235 Let’s look at how we specify the items our web package should include. Either right-click on your project and select the Package/Publish Settings, or open up the project Properties window and select the Package/Publish tab (Figure 10-2). Figure 10-2. Package/Publish settings I would like to bring your attention to the IIS settings option (disabled at the time of writing) that allows you to specify IIS settings should you require. VS2010 also contains support for deploying SQL databases and running SQL scripts which are configured on the Deploy SQL tab (Figure 10-3). CHAPTER 10 ASP.NET 236 Figure 10-3. Deploy SQL settings When you have configured these settings to your liking, right-click on your project and select the Package option. VS2010 will then create your package at ~/obj/YOURBUILDCONFIGNAME/Package. Note that MSDeploy is even nice enough to create a command script that will deploy your application. CHAPTER 10 ASP.NET 237 One-Click Publishing One-click publishing uses IIS remote management services to allow you to publish your application to a remote server with one click. One-click publishing only deploys files that have changed, so is very efficient. To use one-click publishing, you will need a hoster that supports One Click (at the time of writing Discount ASP or OrcsWeb) or, if deploying to your own server, to have IIS remote management services enabled (http://technet.microsoft.com/en-us/library/cc731771(WS.10).aspx). One Click is also only available for projects in the VS2010 format. Before we can use one-click publishing we need to configure a number of settings. Right-click on your project and select the Publish option. You will see a screen similar to Figure 10.4. Figure 10-4. One-click publishing profile settings Fill in the details as shown (using MSDeploy Publish as the Publish Method) and click Close. Once these details are completed, you can publish your application by selecting the Publish Profile you want to use from the drop-down menu (top left-hand side of the VS2010 screen) and clicking the Publish button (Figure 10-5). Note if this tool bar is not showing, right-click on the toolbar and select Web One Click Publish. VS2010 allows a single project to contain up to 50 different one click publishing profiles. CHAPTER 10 ASP.NET 238 Figure 10-5. Initiating one-click publishing ViewState ViewState is the mechanism by which ASP.NET stores the state of controls on a web form. This information is held in a hidden form value called __VIEWSTATE. Depending on the page’s content, ViewState can get pretty large, and is often unnecessary for controls that don’t change such as labels. ASP.NET 4.0 gives you the ability for controls to inherit ViewState settings from parent controls by using the new ViewStateMode property. This makes it very easy to efficiently set ViewStateMode on a large number of controls. ViewStateMode has three settings • Enabled (ViewState used) • Disabled (ViewState not used) • Inherit (ViewStateMode is inherited from parent control) The following example shows how to make lbl1 Label inherit pnlParent's ViewStateMode. <asp:Panel ID=”pnlParent” runat=server ViewStateMode=Disabled> <asp:Label ID="lbl1" Text="text" ViewStateMode=Inherit runat="server" /> </asp:Panel> . and most valuable changes in ASP .NET 4. 0 is the new deployment functionality. VS 201 0 makes it easier than ever before deploy your ASP .NET application. VS 201 0 allows you to • Perform transformations. but VS 201 0 allows you to carry out much more complex changes. VS 201 0 offers 3 options to locate the item you want to change (full details at http://msdn. microsoft.com/en-us/library/dd465326%28VS. 100 %29.aspx):. require. VS 201 0 also contains support for deploying SQL databases and running SQL scripts which are configured on the Deploy SQL tab (Figure 10- 3). CHAPTER 10 ASP .NET 236 Figure 10- 3. Deploy