This blog explains the 2 different ways of deploying the web application to the IIS server.
We often come across the scenarios where in the application's web.config need to have different settings when it is deployed from one environment to the other (Example: From development to staging or production).
We can use either of the below technique to automate the process of changing web.config during deployment:
Below is the list of a few settings which we tend to change while deploying application from one environment to the other:
Web.config tranformation:
This technique specifies how the web.config file should be changed based on the specific build configurations:
The web.config of the above build configuration makes use of the XML-Document-Transform namespace defines two attributes:
To achieve the above requirements using the web.config transformation technique, we need to follow the below steps:
<appSettings>
<add key="Title" value="Titletest"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
<connectionStrings>
<add name="testConnectionString"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<customErrors mode="On" defaultRedirect="GenericError.htm" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
<system.webServer>
<modules xdt:Transform="SetAttributes" runAllManagedModulesForAllRequests="false">
On Visual Studio 2012 and above You can go to Build-àPublish Selection and select the below setting:
Once the .zip package gets created in the above mentioned location, You can either move the package manually to the destination server and import it using the IIS GUI (shown below) or use the web deploy and option in Visual studio and publish it directly.
Web Deploy parameters:
This technique is useful when you have to create a package when you do not know what values need to be set until you deploy the application to the different environment. Moreover this technique also helps when the same application (package .zip file) needs to be placed in different environments with different values.
To achieve the above requirements using the Web Deploy parameters technique, we need to follow the below steps:
You can also import the package using the msdeploy command:
msdeploy.exe -source:package='C:\WebDeploy_Demo.zip' -dest:auto,computerName='<destination server>',includeAcls='False' -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:"C:\WebDeploy_Demo.SetParameters.xml"
If you use IIS Manager to import the package from the IIS GUI:
Once imported you are prompted to enter a value for the parameter in the Enter Application Package Information dialog box. The dialog box displays the name, description, and default value that you specified, as shown below:
Below is the sample content placed in the parameters.xml which will achieve the requirement:
<?xml version="1.0" encoding="utf-8" >
<parameters>
<parameter name="connectionstring"
description="Changing the SQL connectionString to point to the production DB"
defaultValue="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
tags="SqlConnectionString">
<parameterEntry scope="\\web.config$"
match="/configuration/connectionStrings/add[@name='mstestConnectionString1']/@connectionString" />
</parameter>
<parameter name="appsettings"
description="Changing the appsettings value"
defaultValue="Titletest"
tags="" >
<parameterEntry
scope=\\web.config$
match="/configuration/appSettings/add[@key='Title']/@value" />
</parameter>
<parameter name="Debug value"
description="Changing the debug value in web.config"
defaultValue="false"
tags="">
<parameterEntry
scope=\\web.config$
match=/configuration/system.web/compilation/@debug />
</parameter>
<parameter name="runAllManagedModulesForAllRequests setting for modules"
description="Change the runAllManagedModulesForAllRequests in web.config"
defaultValue="false"
tags="">
<parameterEntry
scope=\\web.config$
match=/configuration/system.webServer/modules/@runAllManagedModulesForAllRequests />
</parameter>
</parameters>
Author: Naveen Baliga
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.