Transform config in asp.net mvc isnt updated - c#

I have asp.net mvc 4 project where try to use transform config, where have some default values and when I try to change to release mode some default values change to release values, but it isnt work. When I try to change to release mode and build application I have nothing changes. Here is my default values in Web.config:
<appSettings>
<add key="appId" value="####"/>
<add key="appSecret" value="####"/>
<add key="hostName" value="####"/>
</appSettings>
And here is what I have in my Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</appSettings>
</configuration>
Does anybody help me?

Using xdt:Locator="Match(name)", you're trying to match an attribute name which does not exist. You're looking for key for the appSettings:
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

Related

Multiple int values in config key C# XML

I want to add multiple int values to my Settings.
So far I have this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"
requirePermission="false"/>
</configSections>
<DigiPortServer1>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</DigiPortServer1>
</configuration>
Is this right? I have found many questions considerung multiple String values. How can I access these values? I would like to save these into a int array or something similar.
I would modify your file to be in this way. So it will be easier to access and to work with. This way you can easily access those features by writing ConfigurationManager.AppSettings["3"].ToString();
Note that your file wasn´t having the keys associated with any values. I added the value atribute.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"
requirePermission="false"/>
</configSections>
<appSettings>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</appSettings>
</configuration>
You could access the configuration section value using the ConfigurationManager GetSection method.
var section = System.Configuration.ConfigurationManager.GetSection("DigiPortServer1") as System.Collections.Specialized.NameValueCollection;
var value = section["keyname"];
If section is the name value pair then cast with above type (NameValueCollection) or you can use own type to cast.

XML Custom Configuration Error Handling

Here is some code of my app.config file in my windows form application. I used this configuration file for the printer and scanner. I have used custom configurations in this xml. The problem I'm facing is whenever any attribute value is not set, My application throws a built in message error box like "The port com 4 does not exists"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Server" value="blah blah" />
<add key="Database" value="blah" />
<add key="User" value="blah" />
<add key="StoreID" value="blah" />
<add key="BranchID" value="blah" />
<add key="LotSize" value="100"/>
<add key="ZipsPrintingPath" value="blah.exe"/>
<add key="BCScannerPath" value="blah.exe"/>
<add key="SecuGenDeviceName" value="DEV_AUTO"/>
<add key="SecuGenPortAddress" value="USB_AUTO_DETECT"/>
<add key="SecuGenLiveTimeout" value="15000"/>
<add key="SecuGenImageQuality" value="50"/>
<add key="BCSPort" value="COM4"/>
<add key="BCSBaudRate" value="9600"/>
<add key="BCSParity" value="None"/>
<add key="BCSByteSize" value="8"/>
<add key="BCSStopBit" value="One"/>
<add key="BTicketBarcodeStartsWith" value="%B"/>
<add key="BTicketBarcodeEndsWith" value="."/>
<add key="TechSupportURL" value="blah"/>
</appSettings>
</configuration>
Here is also attached port error.
Now, I want to suppress this message box or I want to show this message in my custom message box.

Strange transform behaviour for web.config when publishing a webapp

I have created a new mvc webapp with the following transforms for web.config:
Web.Debug.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Debug" value="true" xdt:Transform="Insert"/>
</appSettings>
</configuration>
Web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Release" value="true" xdt:Transform="Insert"/>
</appSettings>
</configuration>
Then I created a publish profile and called it Release, but I select the Debug(OBS! important) build configuration.
(I know. Stupid example. In my real project they were called Test and Test2.)
When I run the publish action I get the following in the transformed Web.config:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Debug" value="true"/>
<add key="Release" value="true"/>
</appSettings>
Both the transformations were performed! Strange! If I change the name of the publish profile to Release2 I get following correct result:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Debug" value="true"/>
</appSettings>
What do you think? Bug?
Met same issue and finally was able to identify the reason and resolve it.
This issue happens because of publish profiles configuration settings mess. When you set up profile with configuration manager you should make sure Current Solution Configuration matches Configuration that is going to be used: Easiest way for configuration to track config Transformations
Otherwise, remember, that before selected Profile configuration, selected Configuration Transformation would be applied.
So, make sure you don't have twice assigned Configuration for different profiles. If so, just add one more configuration for 'failed' profile to resolve it and you get what you expected on transformation applied.

Trouble with web.config

I have a configuration in my web.config like this?
<configuration>
<appSettings>
<add key="BASE_URL" value="/mvc/" />
</appSettings>
</configuration>
in my web.Release.config, I have this:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="BASE_URL" value="/mvc" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
but, when I call BASE_URL property, it's return "/mvc/" and not "/mvc"
I using the Release Configuration to build my project
I using C# MVC4 in VS 2013

Is there anything wrong with ConfigurationManager in C#?

I read many topics related to configuration manager but could not resolve the issue. I just want to read the connection string as well some appsetting keys from a CLASS LIBRARY in the web application.
I have reference to System.Configuration Class.
This is my code:
using System.Configuration;
...
string constr = ConfigurationManager.ConnectionStrings["cbuddydb"].ConnectionString;
string strUserName = ConfigurationManager.AppSettings["username"];
string strPwd = ConfigurationManager.AppSettings["password"];
But it seems reading from a different config file. not from the web.config in my project. Because the value read is wrong.
My web.config is below:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.data>
<connectionStrings>
<clear />
<add name="cbuddydb" connectionstring=
"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;Persist Security Info=True;
User=#username;Password=#password;Option=3" providerName="MySql.Data.MySqlClient" password=""/>
</connectionStrings>
<appSettings >
<clear />
<add key="username" value ="6/0RUNnSmUBsbdNoCg+9Sw=="/>
<add key="password" value =""/>
</appSettings>
</system.data>
</configuration>
The reason for this is because of configuration file inheritance. The connection string at index 0 may not be in your config file, but it may have been inherited from machine.config etc. Have a look at ASP.Net config file hierarchy and inheritance: http://msdn.microsoft.com/en-us/library/ms178685.aspx
You could clear the inherited connection strings by specifying the following in your web.config
<connectionStrings>
<clear />
<add name=”MyConnString” connectionString=“Whatever“ />
</connectionStrings>
EDIT: In your config, place your connectionStrings and appSettings tags directly below the configuration element. They should not be within the system.data element. They are direct children of the configuration element. And remove the extra password attribute after the providerName. I can't validate your connection string, since I don't know how you're using it.
<configuration>
<connectionStrings>
<clear />
<add name="cbuddydb" connectionString=
"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;Persist Security Info=True;
User=#username;Password=#password;Option=3" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<appSettings >
<add key="username" value ="6/0RUNnSmUBsbdNoCg+9Sw=="/>
<add key="password" value =""/>
</appSettings>
<system.data>
....
You should consider encrypting sensitive information in your config file, like passwords.

Categories