Web.config transform not transforming VS 2017 - c#

I have literally uncommented the boilerplate code and tried to publish to the file system to check if the "transform" works.
However, it does not transform the web.config file. I have look at the some articles and answers on here but cannot get it to work.
Things I tried:
Removing the namespace from the configuration node(complains about xdt missing)
Creating a new transform file(production)
Creating a new configuration(production)
Here is the web.Release.config:
<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=301874 -->
<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>
-->
<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>
</configuration>
I get the following error:
No element in the source document matches '/configuration/connectionStrings/add[#name='MyDB']'
But as you can see it is present in the add node.
Any suggestions what to do? I am confused why it isn't working.
Edit:
web.config connection string
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;database=smartDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

The value of the name attribute needs to match between your web.config and web.release.config, since you are specifying Match(name) in the transformation config.
In your web.config you have: name="DefaultConnection"
In your web.release.config: name="MyDB"

Related

How to set system.web compilation debug mode off via VSTS

I have this web.config file with the compilation option set as below:
<configuration>
...
<system.web>
<compilation debug="true" targetFramework="4.5" />
</system.web>
</configuration>
How to turn off the compilation's debug flag to false via VSTS's Build Definitions?
Update:
I'm deploying the system to Azure server by VSTS's release function.
You can use transformation to achieve what you want.
here is the documenation:
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations
Here is how your Web.debug.config file should look. You can also use web.release.config if you want to use transform in release mode.
<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=301874 -->
<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 atrribute "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 debug="false" xdt:Transform="Replace"/>
<!--
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>
</configuration>

Config file structure

I got mixed with app.config file.
I had config that failed to get connectionStrings:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<connectionStrings>
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ProgrammingInCSharpConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ProgrammingInCSharp;" />
</connectionStrings>
</configSections>
</configuration>
Problem was solved after placing <connectionStrings> outside of <connectionStrings> :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections />
<connectionStrings>
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ProgrammingInCSharpConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ProgrammingInCSharp;" />
</connectionStrings>
</configuration>
But I found many samples with pattern below
<configSections>
<connectionStrings>
...
</connectionStrings>
</configSections>
It might work in some cases?
What is logic of configuration file in general and what kind of information might be located in each section?
No. It is not possible to have a valid app.config file and yet having a connectionStrings in your configSections node.
According to MSDN, this is the list of allowed elements:
section
sectionGroup
add
clear
connectionStrings isn't in the list and I don't expect an application to find the connection strings in that place.
The configSections node contains configuration section per namespace declarations, but is not usually used for connection strings, unless they are namespace - specific.
But I found many samples with pattern below
The patterns you found could never work since <connectionStrings> is not allowed to be nested directly under <configSections>.
This could be achieved, by stating a connection string variable under the <section> itself, and if the server code targets them via a specific namespace.
An example:
<configuration>
<configSections>
<section name="section1" type="System.MyNameSpace" />
</configSections>
<section1 connString="...conn.." />
</configuration>
In the example above the namespace System.MyNameSpace will now have a variable (field) called connString, so it can target it in code behind.
For conclusion, if your connection strings are not per namespace, the <connectionStrings> element should be nested directly under the <configuration> node and no where else.

web.config transform - delete comments from connectionstring section

I store several different connection strings in my web.config for development and testing. All but one is commented out so I can change info as needed.
When I publish, I would like to replace everything (including comments) in the connectionStrings node with this:
<add name="myDb" connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};" providerName="System.Data.SqlClient" />
<!--<add name="myDb" connectionString="Data Source={SERVER};Initial Catalog=ManEx;Integrated Security=True" providerName="System.Data.SqlClient" />-->
I know how to change the active string with this:
<add name="myDb"
connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};"
providerName="System.Data.SqlClient"
xdt:Transform="Add"
xdt:Locator="Match(name)"/>
But I don't know how to clear out the comments I don't want and add the comment I do want.
Any ideas?
Instead of transforming the string, or using "Remove" and "Insert" clean the section try using "Replace".
For example:
<connectionStrings xdt:Transform="Replace">
<add name="myDb"
connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};"
providerName="System.Data.SqlClient" />
</connectionStrings>
You can configure this section exactly how you want it, even if that means you add new comments.
What I did based on this answer was the following:
Removed the existing connectStrings section in Web.config which contains commented out connection strings used during debug time;
Re-added the connectionStrings section with the correct connection string to be used when the app is deployed.
So in your Web.config transform file you have something like this:
<!-- Removes the existing connectionStrings section which contains internal connection strings used for debugging -->
<connectionStrings xdt:Transform="Remove">
</connectionStrings>
<!-- Re-adding the existing connectionStrings section -->
<connectionStrings xdt:Transform="Insert">
<add name="MyConnectionStringName" connectionString="Data Source=CLUSTERSQL;Initial Catalog=MyDatabase;Integrated Security=True;multipleactiveresultsets=True" providerName="System.Data.SqlClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
In Visual Studio 2013, you can have several Web.config files.
Also, when you create a new project, VS creates 2 for you : Web.Debug.config and Web.Release.config. That way, you can have a different Web.Config for your debug project and for your release project
If you need to Add/Insert/Setattributes inside a replaced connectionstring (for example if you use deployments) you can nest the transformations to remove the comments and replace the attributes:
<connectionStrings xdt:Transform="Replace">
<add name="connectionDatabase"
connectionString="#{ConnectionString}"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)" />
</connectionStrings>

Hardcoding into web.config in asp.net

Below specified is c# code and i am using asp.net with c#,The below code describes the date difference it will update the table after 2 months,
cmd3.CommandText = "update TrackingFaculty_det "
+ "SET Type=#Type WHERE "
+ "DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,#Today)>60";
I need to hardcode the month value into web.config,so that if i want to change the date difference to three months or so on,so that in web.config if i make a change then the changes will occur in every c# pages where i have these lines of code.
My Web.Config is as below
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<appSettings/>
<connectionStrings>
<add name="ProjectConnectionString" connectionString="Data S ource=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="ProjectConnectionString2" connectionString="Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="ProjectConnectionString3" connectionString="Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="Microsoft.Office.Interop.Word, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/></assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
Add key in web.config like this
<configuration>
....
<appSettings>
<add key="Difference" value="60"/>
</appSettings>
....
</configuration>
get key from web.config like this
string str_Diff = WebConfigurationManager.AppSettings["Difference"].ToString();
cmd3.CommandText =
"update TrackingFaculty_det "
+ "SET Type=#Type WHERE "
+ "DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,#Today) > " + str_Diff;
Web.config code
<configuration>
....
<appSettings>
<add key="Difference" value="60"/>
</appSettings>
....
</configuration>
get key from web.config
string str_diff = Convert.ToString(ConfigurationManager.AppSettings["Difference"]);
cmd3.CommandText = "update TrackingFaculty_det SET Type=#Type WHERE (FID=#FID) and DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,#Today)>'"+str_diff+"'";
This is what i needed and i got the solution

MVC multiple db connection and appsettings

I wonder if anybody know a solution to achieve this.
Basically, I need to build a website like -
1) www.domain1.com/subApp1 goes to one database and one appsettings.
2) www.domain1.com/subApp2 goes to one database and one appsettings.
In Webform application - I point the main domain (www.domain1.com) to a folder (landing) with a web.config which contains
<location path="subApp1">
<connectionStrings>
<clear/>
<add name="SchemaConnection" connectionString="data source=dev;initial catalog=Schema;user id=sa;password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="brandName" value="subApp1"/>
<add key="ImageLocation" value="~/uploaded/subApp1/"/>
</appSettings>
</location>
<location path="subApp2">
<connectionStrings>
<clear/>
<add name="SchemaConnection" connectionString="data source=dev;initial catalog=Schema2;user id=sa;password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="brandName" value="subApp2"/>
<add key="ImageLocation" value="~/uploaded/subApp2/"/>
</appSettings>
</location>
That works fine. But in MVC, It ain't working. I hope there is a way to do in MVC to achieve this. Please advice if anyone know.

Categories

Resources