web.config transform not transforming - c#

I am publishing a website app to an Azure App Service. I've created a QA configuration profile and added a matching Web.QA.config file. Here's the entire contents (with some redacted) of that file:
<?xml version="1.0"?>
<!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<!-- In case configuration is not the root element, replace it with root element in source configuration file -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="ida:ClientId" value="9a...54e" xdt:Transform="SetAttributes" xdt:Locator="Match(value)" />
<add key="ida:ClientSecret" value="vuW...z3kY=" xdt:Transform="SetAttributes" xdt:Locator="Match(value)" />
<add key="ida:PostLogoutRedirectUri" value="https://....azurewebsites.net/" xdt:Transform="SetAttributes" xdt:Locator="Match(value)" />
</appSettings>
</configuration>
When I publish the site to the Azure App Service using the QA profile, these values are not being transformed.
What am I doing wrong?

You're matching on value instead of key.
Use xdt:Locator="Match(key)"

Related

ASP.NET Web.Debug and Web.Release file transformations

First of all I know there are several pages about this issue e.g. Web.Config Debug/Release, Web.config Transformation Syntax now generalized for any XML configuration file and Web.config File Transformations. But most of them are outdated and does not mentioned clearly about all of the three files: Web.config, Web.Debug.config, Web.Release.config.
So, assume that I have the following settings for Web.config:
Web.config:
<appSettings>
<add key="ClientId" value="xxxxx"/>
<add key="ClientSecret" value="xxxxx"/>
</appSettings>
And I want to use these settings in debug and release in the following ways:
Web.Debug.config:
<appSettings>
<add key="ClientId" value="ddddd"/>
<add key="ClientSecret" value="ddddd"/>
</appSettings>
Web.Release.config:
<appSettings>
<add key="ClientId" value="rrrrr"/>
<add key="ClientSecret" value="rrrrr"/>
</appSettings>
1) What is the procedures to perform this accurately? I think while debugging and publishing, these settings are used automatically according to my selection Debug or Release in Visual Studio run and publish dialog. Is that true?
2) Should I remove these settings from Web.config after moving to Web.Debug.config and Web.Release.config?
3) What is the Test selection in the Configuration field of the Publish dialog in VS?
Any help would be appreciated.
I would recommend reading an overview of how web.config transforms work:
https://blog.elmah.io/web-config-transformations-the-definitive-syntax-guide/
In general, the Web.*.config files will make changes to the Web.config file depending on the selected publish configuration in Visual Studio. For example, if you want to update/replace a value in a debug publish, your Web.Debug.config file should look like:
<configuration xmlns:xdt="...">
<appSettings>
<add key"ClientId" value="ddddd" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
<add key"ClientSecret" value="ddddd" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
Here is the current Microsoft documentation on how these work:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-3.1

Why my app does not load correctly on local server?

I'm setting up a new app on a server on my work and when I access to the ip address on the explorer shows the default app
Default app
but when I try to access to the app that i created shows this error
My app
This is my web.config file
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="Default.aspx" />
</files>
</defaultDocument>
</system.webServer>
<system.web>
<compilation targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<pages>
<controls>
<add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
</controls>
</pages>
</system.web>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
</configuration>
This are my files
Files of application
I tried a lot of solutions, like permitions on folders on the IIS manager, enabling anonymous access, modifying web.config file, modifying the AutoEventWireup="true|false " etc but all of them leads me to the same error, can it be a compilation error? I am publishing via file system and pasted it on the wwwroot directory
Also I am using master pages except for the first page (the login)
With the configuration you are showing you most likely need to configure a virtual directory in IIS for your application to run in.
I solved my problem (not sure what I did diferent than before but its working now)
Instead of adding another website I added an application on the default website
and that solved the problem
Thanks a lot

appSettings in Web.config and Web.Release.config

Trying to get simple <appSettings> for dev vs. prod.
My Web.config:
<appSettings>
<add key="hello" value="debug" />
</appSettings>
My Web.Release.config:
<appSettings>
<add key="hello" value="prod" />
</appSettings>
(both under <configuration>)
When I have it in Debug mode, and run my MVC site, I can do a simple return Content(WebConfigurationManager.AppSettings["hello"]); in my HomeController.Index and it returns dev. If I switch the mode to Release it still returns dev. I'd like to simulate prod mode without actually publishing to prod.
In the build-specific Web.config file, you have to tell it how to transform the base .config file. So to do what you ask, your Web.Release.config file should look like this:
<appSettings>
<add key="hello" value="prod" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
In the above code the SetAttributes transform will change the attributes of any element that matches the key attribute containing the value hello.
Starting from .NET 4.7.1 feature called Configuration builder is supported which gives developer ability to load configuration not only from Web.Release.Cong but basically from any source. Read more about .NET Framework 4.7.1 ASP.NET and Configuration features

ASP.NET 5 kre-version when deploying to Azure via BitBucket

I'm using new ASP.NET 5 and I've faced a problem.
I have ASP.NET MVC 6 application. Source code I keep on BitBucket and Azure is my hosting. I linked BitBucket with Azure such that on every push, deploy is launched.
Everything was working great until I updated KRE version to 1.0.0-beta2 (was 1.0.0-beta1).
Now after auto deploy I'm getting 500 Internal Server Error. But when I publish my application from Visual Studio - it is working without errors.
After investigating files on Azure via FTP I've noticed that when I deploy from Visual Studio my web.config in wwwroot looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta2" />
<add key="kre-package-path" value="..\approot\packages" />
<add key="kre-version" value="1.0.0-beta2" />
<add key="kre-clr" value="CLR" />
<add key="kre-app-base" value="..\approot\src\Web" />
</appSettings>
</configuration>
But after auto deploy from BitBucket it looks like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta2" />
<add key="kre-package-path" value="..\approot\packages" />
<add key="kre-version" value="1.0.0-beta1" />
<add key="kre-clr" value="CLR" />
<add key="kre-app-base" value="..\approot\src\Web" />
</appSettings>
</configuration>
kre-version key is different.
Why is it putting beta1 there? Is there any way to specify kre-version explicitly?
Looks the same as Deploying ASP.NET vNext beta 2 on Azure with Kudu. Can you try the workaround mentioned there, which is to set a couple app settings (in the Azure portal for the site):
SCM_KRE_Version=1.0.0-beta2
SCM_KRE_CLR=CoreCLR

Split 'appSettings' section in several web.config files

I'm working on a ASP.NET project and I need to add some settings in appSettings section of my web-app.
Now these settings are growing up, so I'd like to organize them in different files. I've created other web.config files in different directories of my application, adding something like this:
<?xml version="1.0"?>
<configuration>
<system.web>
</system.web>
<appSettings>
<add key="settingKey" value="settingValue" />
</appSettings>
</configuration>
But when I try to access them via ConfigurationManager.AppSettings["settingKey"], I get null.
So, is it possible to split settings in different files? Is there another way to logically organize app settings values?
I know this is too old and probably doesn't even apply to .NET Core but for those coming from Google and using non-.NET Core json config files. Here's what I normally do...
I use configSources to take all config settings out of the web.config. This allows you to a specific config section to a different file by providing a relative location for example here's how you'd declare a configSource in a configuration section (in the root web.config file)...
<configuration>
<log4net configSource="Config\debug\log4net.config" />
<appSettings configSource="config\debug\settings.config" />
<connectionStrings configSource="config\debug\connections.config" />
...
</configuration>
You can name those files whatever you want, just make sure that the path specified and files exist in the solution. Here's what the settings.config file looks like...
<?xml version="1.0"?>
<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="foo" value="bar" />
</appSettings>
Now, the relative path is relative to the project root...
In picture above you can see that I have provided two different paths for different deployment environments, that's because obviously my conection strings and settings are different in production.
Then you can use configuration transformations so that the application can use the correct config files whether it is in debug or release mode...
This is what the Web.Debug.config file looks like...
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<log4net configSource="Config\debug\log4net.config" xdt:Transform="Replace" />
<appSettings configSource="config\debug\settings.config" xdt:Transform="Replace" />
<connectionStrings configSource="config\debug\connections.config" xdt:Transform="Replace" />
</configuration>
The release one is pretty much the same...replace the paths provided to the configSource attributes.And that's pretty much it.
There are other web.config elements that support configSource settings such as many of the system.serviceModel children element.
You will only be able to see the web.config settings within a directory if the currently executing path is within that directory.
So for example:
/MyDirectory/web.config
is only visible if you are loading a page like:
/Mydirectory/MyTestPage.aspx
You would not see the web.config settings here for example:
/OtherDirectory/MyTestPage.aspx
This post may help:
Creating a custom .config file in asp.net

Categories

Resources