Sections must only appear once - appsettings - asp.net

I'm a bit confused as to how I can specify another .config file in my web.config while retaining parts of the original web config. I want to put my connection strings in another file but when I build the project I get an error about there being multiple appsettings elements.
I have this:
<appSettings file="ConnectionStrings.config">
</appSettings>
then further down, because it's a Crystal Reports application, these settings are specified. I don't want these keys in my connnectionstrings.config file as they're not relevant.
<appSettings>
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>
How do I keep my seperate config file and the Crystal settings above, without putting them all in the connectionstrings.config file?

You main configuration file(web.config) should look like this
<?xml version="1.0"?>
<configuration>
<!--other sections-->
<appSettings file="appSettings.config">
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>
<!--other sections-->
</configuration>
Further your separate appSettings.config should look like this
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="YourConnectionStringValue" value="" />
</appSettings>
This is how we have worked in our project.

Try this, maybe!
<appSettings file="ConnectionStrings.config">
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>

Related

How to Update the .config file using Azure DevOps "Update config" Task

I want to Update the Web.config file using Azure DevOps Release Pipeline.
I am using [Update Config][1] Task from MarketPlace.
[1]: https://marketplace.visualstudio.com/items?itemName=digitalmedia34.updateconfig
Here Is the Web.config File
<!-- Some Contents -->
<configuration>
<appSettings>
<add key="Setting1" value="local setting"/>
<add key="CommonSetting" value="local common setting"/>
</appSettings>
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=LocalSQLServer;Initial Catalog=MyReleaseDB;User ID=xxxx;Password=xxxx" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.7.2"/>
<httpRuntime targetFramework1="4.7.2"/>
</system.web>
</configuration>
I am able to change the value of some configuration by adding variables like [Variables]
[2]: https://i.stack.imgur.com/uq85Y.png
My Question is how can I change the values of Nested Property. For Example if I want to change the targetFramework in
<system.web>
<compilation debug="true" targetFramework="4.7.2"/>
<httpRuntime targetFramework1="4.7.2"/>
</system.web>
Please let me know how can I update these values
Thanks in advance
How to Update the .config file using Azure DevOps "Update config" Task
You could use the task Replace Tokens to update the key's values,
the format of variable in .config file is #{TestVersion}#:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Setting1" value="local setting"/>
<add key="CommonSetting" value="local common setting"/>
</appSettings>
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=LocalSQLServer;Initial Catalog=MyReleaseDB;User ID=xxxx;Password=xxxx" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="#{TestVersion}#"/>
<httpRuntime targetFramework1="#{TestVersion}#"/>
</system.web>
</configuration>
Use Replace Tokens task to update the key's values:
And define the key's values on the Variables.

Do Nuget.config credentials get cached? And where?

First I add my package source (test).
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="test" value="https://mytestpackagesource" />
</packageSources>
</configuration>
When I run dotnet restore I get a 401 (Unauthorized), which is expected. So I add credentials.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="test" value="https://mytestpackagesource" />
</packageSources>
<packageSourceCredentials>
<test>
<add key="Username" value="apparently this can be anything when you use a token?" />
<add key="ClearTextPassword" value="my personal access token" />
</test>
</packageSourceCredentials>
</configuration>
And this works. But if I change the token to something invalid or even remove the packageSourceCredentials element completely it still works, which makes me believe my token got stored somewhere else. So my question is where did it get stored and how do I update it?

How to encrypt a file linked to a web.config

I have a web.config file that links to a common.config file. common.config is being used by multiple applications. I used the aspnet_regiis.exe, but that only encrypts the web.config file. How can i encrypt the common.config file?
web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="C:\Users\naem\Documents\common.config" />
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
common.config file:
<?xml version="1.0" encoding="utf-8"?>
<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="myKey" value="This is the Value!!!!"/>
</appSettings>
I don't have the rep to comment on the original post, but this looks like a duplicate of How to encrypt .config file Asp.net
I found workaround to solve this issue. Hope it helps.
Rename your common.config to web.config temporarily.
Add configuration as root element to this file. So your common.config will look like as follows.
<configuration>
<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="myKey" value="This is the Value!!!!"/>
</appSettings>
</configuration>
Run encrypt command.
aspnet_regiis.exe -pef appSettings "C:\Users\naem\Documents"
Open encrypted file and remove Configuration tags.
Rename file to Common.config

Nuget Package - Web.config.transform Add

I'm creating a NuGet package and want it to update the web projects Web.Config file with certain settings. I am using the web.config.transform to edit the web.config file of an application. It's working well when I simply add appSettings - like so:
<configuration>
<appSettings>
<add key="WebPToolFolder" value ="~/Tools"/>
<add key="ImagesFolder" value ="~/Content/themes/base/images"/>
</appSettings>
</configuration>
However, if I try an add to the staticContent it doesn't seem to alter the tags. For example, here is the web.config.transform file:
<configuration>
<appSettings>
<add key="WebPToolFolder" value ="~/Tools"/>
<add key="ImagesFolder" value ="~/Content/themes/base/images"/>
</appSettings>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webp" mimeType="image/webp" />
</staticContent>
</system.webServer>
</configuration>
It updates the appSettings, but not the staticContent tags - any ideas?
Old question but if anyone lands on it the following should work:
In your case to add/update the staticContent element:
It's an alternative solution, so you won't use the .transform file, but rather the web.config.install.xdt (and web.config.uninstall.xdt) which I find better:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!-- some other elements -->
<staticContent xdt:Transform="InsertIfMissing">
<mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Locator="Match(fileExtension)" xdt:Transform="InsertIfMissing" />
</staticContent>
<!-- some other elements -->
</configuration>
This way you don't need to do any pre-update preperations, just upgrade the package.
Check this post for XDT support from Nuget 2.6 onwards.
You need to put an empty <staticContent></staticContent> in your web.config and then use the xdt:Transform="Insert" on the element like this:
Your web.config:
<configuration>
<appSettings>
<add key="WebPToolFolder" value ="~/Tools"/>
<add key="ImagesFolder" value ="~/Content/themes/base/images"/>
</appSettings>
<system.webServer>
<staticContent>
</staticContent>
<system.webServer>
</configuration>
And then you can insert a value in your transform file like this:
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Transform="Insert"/>
</staticContent>
</system.webServer>
Took me a while to find out. Hope this helps.
Have you tried adding an xdt:Transform="Replace" attribute to the tags you want to update?
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="WebPToolFolder" value ="~/Tools" xdt:Transform="Replace"/>
<add key="ImagesFolder" value ="~/Content/themes/base/images" xdt:Transform="Replace"/>
</appSettings>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Transform="Replace"/>
</staticContent>
</system.webServer>
</configuration>
There's some good Microsoft documentation here
If you post the initial markup and what you want it to look like maybe we could help a bit more :)

Moving <httpredirect> out of web.config in separate config file

We have many (more than 100) redirects in our web.config like
<configuration>
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="/a" destination="/a/dfdf/default.htm" />
<add wildcard="/sad" destination="/aasd/dfdf/defsadault.htm" />
<add wildcard="/asdsaa" destination="/aasdas/dfasddf/default.htm" />
<add wildcard="/aasdsa" destination="/asdsaa/dfdf/defsdault.htm" />
<add wildcard="/aasd" destination="/adsa/dfdf/default.htm" />
..... more than 100
</httpRedirect>
</system.webServer>
</configuration>
Is there way we can have this section managed in separate web.config or any other best solution?
You can move some config elements into their own config file to reduce clutter in the web.config.
<configuration>
<system.webServer>
<httpRedirect configSource="httpRedirects.config" />
</system.webServer>
</configuration>
This is achieved by adding the configSource attribute as shown above.
And in your seperate httpRedirects.config
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="/a" destination="/a/dfdf/default.htm" />
<add wildcard="/sad" destination="/aasd/dfdf/defsadault.htm" />
<add wildcard="/asdsaa" destination="/aasdas/dfasddf/default.htm" />
<add wildcard="/aasdsa" destination="/asdsaa/dfdf/defsdault.htm" />
<add wildcard="/aasd" destination="/adsa/dfdf/default.htm" />
</httpRedirect>
Note I have only tried this with other config elements.
You can store that in Separate Config file as shown here: SectionInformation.ConfigSource Property
In order to avoid cluttering the configuration file - web.config - it can be defined in a separate configuration file. That file can then be referenced from the web.config file as below:
<httpRedirect configSource="httpRedirects.config" />
The configSource attribute tells IIS configuration that the <httpRedirect> section is defined in a separate file httpRedirects.config.
EDIT:
Please make sure you have httpRedirect attribute set to enabled=true as the default value is false.
<httpRedirect enabled="true" configSource="httpRedirects.config" />

Resources