Visual Studio 2010 : How to deploy a Web application with Web Application Project Deployment (Part 2)
In all project it’s a real nightmare to manage configuration files. In Silverlight projects like in web application, you can take advantage of Web Application Project Deployment features of Visual Studio 2010. In this blog, I’ll explain you :
- How to create a new configuration (part 1);
- How to create package definition (part 1);
- How to create package (part 1);
- How to publish the application (part 1);
- How to use transformation with web.config files (part 2);
- Some more links on this subject (part 2).
In this post, I won’t explain SQL deployment.
How to use transformation with web.config files
In this post, I’ll explain how to use Web.config Transformation Syntax for Web Application Project Deployment. I’ll demonstrate this mechanism with two samples:
- transformation for debug configuration;
- transformation for staging configuration;
In your web.config file, you will define all parameters that you need in your application. For example, you need a DB connection string named MYDB. You need to define the connectionString add element with a specific name but without any connectionString value. You can also define, for example, compilation target to framework 4.0, debug mode. Finally, you disable custom errors.
Code Snippet
- <?xml version="1.0"?>
- <configuration>
- <connectionStrings>
- <add name="MyDB"connectionString="..." />
- </connectionStrings>
- <system.web>
- <compilation debug="true" targetFramework="4.0" />
- <customErrors mode="Off"></customErrors>
- </system.web>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- </system.webServer>
- </configuration>
This Web.Config file will be used/transformed to be compliant with the desired configuration.
Web.Debug.config
In the Web.Debug.Config file, you’ll create some transformation with a specific syntax. In this example, I’ll just add a specific connectionString dedicated to debug environment. In the Add element, you just need to add xdt:Transform and wdt:Locator=”Match(name)”. Those two attributes will inform that the transformation will be applied on an “add” xml element where the attribute name=”MyDB”, connectionString attribute will contains the specific connection string.
Code Snippet
<?xml version="1.0"?>
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=MySQLServer;Initial Catalog=MyDevDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
</system.web>
</configuration>
Here is the result of the translation after compiling and publishing the application.
Code Snippet
- <?xml version="1.0"?>
- <configuration>
- <connectionStrings>
- <add name="MyDB"connectionString="Data Source=MySQLServer;Initial Catalog=MyDevDB;Integrated Security=True" />
- </connectionStrings>
- <system.web>
- <compilation debug="true" targetFramework="4.0" />
- <customErrors mode="Off"></customErrors>
- </system.web>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- </system.webServer>
- </configuration>
Web.Staging.config
You could also make some replacement or element removal and of course another DB connectionString replacement:
Code Snippet
<?xml version="1.0"?>
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=MySQLServer;Initial Catalog=MyStagingDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>
Here is the result of the translation.
Code Snippet
- <?xml version="1.0"?>
- <configuration>
- <connectionStrings>
- <add name="MyDB"connectionString="Data Source=MySQLServer;Initial Catalog=MyStagingDB;Integrated Security=True" />
- </connectionStrings>
- <system.web>
- <compilation targetFramework="4.0" />
- <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly">
- <error statusCode="500" redirect="InternalError.htm"/>
- </customErrors>
- </system.web>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- </system.webServer>
- </configuration>
This mechanism is very helpfull, hoping that this helps.
Some more links on this subject
ASP.NET Web Application Project Deployment Overview
https://msdn.microsoft.com/en-us/library/dd394698(VS.100).aspx
Web.config Transformation Syntax for Web Application Project Deployment
https://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx
Web Deployment: Web.Config Transformation
https://blogs.msdn.com/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx