obb-taurus
obb-taurus

Reputation: 857

How to deploy ASP.NET application to production machine without having to change DB connection string manually

As it stands, right now when I deploy my web application I always go into my web.config file to change the server name etc from the connection string manually before deploying the application. Is there an easier way to deploy a web app without having to always change the server in the connection string?

Thanks

Upvotes: 1

Views: 118

Answers (4)

Sam Hobbs
Sam Hobbs

Reputation: 2881

Assuming you only need to change the connection string for just one specific Web Deploy then you can do that with a transformation as others have said. The following should show exactly what you need to do.

In Solution Explorer expand the Properties node to get the PublishProperties as in the following.

enter image description here

Right-click on the Web Deploy profile and select Add Config Transform as shown in the following.

enter image description here

You will get a Web.project - Web Deploy.config file in the Web Config node. The initial contents will be the following.

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <!--
      In the example below, the "SetAttributes" transform will change the value of 
      "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
      finds an attribute "name" that has a value of "MyDB".

      <connectionStrings>
        <add name="MyDB" 
          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)" />
  <!--
    In the example below, the "Replace" transform will replace the entire 
    <customErrors> section of your web.config file.
    Note that because there is only one customErrors section under the 
    <system.web> node, there is no need to use the "xdt:Locator" attribute.

    <customErrors defaultRedirect="GenericError.htm"
      mode="RemoteOnly" xdt:Transform="Replace">
      <error statusCode="500" redirect="InternalError.htm"/>
    </customErrors>
  -->
</system.web>

Change the sample to the following or add the following.

<connectionStrings>
  <add name="csname"
    connectionString="yourotherconnectionstring"
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

Where csname is the name in the Web.config of your connection string that you need to replace in the deployment.

There are many other transformations possible but if you only need to change the connection string for a specific Web Deploy then that should be the most straight forward. It is saying that you want to search for the connection string that has the specified name and then change the connection string value during the deployment.

Upvotes: 0

Chris Diver
Chris Diver

Reputation: 19802

It depends on how you are deploying your web app, but one common way to do it is to use web.config transforms

http://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx

Upvotes: 1

Abhinav
Abhinav

Reputation: 2085

we use WIX installer exactly for this purpose.

It can be customized, like in my case, to select DEV, QA or PROD env while installation.

Best thing is it uses underlying MSI installation framework, if thats the right word here.

Upvotes: 0

Related Questions