ASP.NET and web.config transforms not working when developing - c#

I am working on an ASP.NET C# project, where I would like to make use of web.config transforms.
I therefor installed the extension "Configuration Transform" and added a Web.Debug.config and Web.Release.config.
Within the Web.config I have not declared anything specific to my application. The debug config contains my testing/developing settings while the release config contains tokens #{someVar}# that later will get replaced by TFS.
When I publish my application the Web.config gets correctly created according to the configuration (debug/release). Also Preview config transform gives the correct result (besides the line breaks).
However when starting the application from within Visual Studio 2017 with debug configuration it complains about missing tags.
Why is that and how can I fix this?
Web.config
<!-- Does not contain the request tag -->
Web.Debug.Config
<request xdt:Transform="Insert">
<mysetting>MyDevelopmentSetting</mysetting>
</request>
Web.Release.config
<request xdt:Transform="Insert">
<mysetting>#{MyTokenThatWillGetReplacedByTFS}#</mysetting>
</request>

Web config transforms do not run in Visual Studio (when you press F5/run the app in VS). They only run on builds when publishing.
Since your web.config doesn't have the setting and the application is expecting it, it's properly complaining about the missing tag.
You will need to add this tag to your web.config.

Related

Can the IIS version be specified in a web.config so as to avoid e.g. "the useKernalMode attribute is not allowed"?

The following shows a section from the web.config file for an application we're running in IIS. I've included it as an image so as to include the green line under useKernelMode, and the error message shown when hovering over it.
(Note: As you can see, this also applies to UseAppPoolCredentials and extendedProtection. I just selected useKernelMode for this example)
The configuration here should be correct - it was set up by an admin who presumably knows what he is doing, and it does seem to work fine. There is also documentation for Windows Authentication which identifies useKernelMode as a valid attribute.
Why then is this marked as an invalid attribute? Different versions of IIS obviously support different elements and attributes in config files - could it be that Visual Studio somehow relates this config file to an older standard for web.confi files, in which the attribute was in fact not valid? If so, is there some way for me to specify that this particular config should be validated according to a specific version of IIS, e.g. IIS 7.5, 8.5, or 10?
After all, I would like any real mistakes in my web.config to be marked this way, but I'd like to avoid (presumably) false positives like this.
I'm afraid it has nothing to do with IIS version. IIS locked it up and set to read only.
As you can see, the section is only allowed by applicationhost.config level.
If you access this in visual studio, you will receive
If you want to fix this, plesae set applicationhost.config.
<section name="windowsAuthentication" overrideModeDefault="Deny" />
to
<section name="windowsAuthentication" overrideModeDefault="Allow" />
After that, VS still report invaild attribute but IIS express and IIS will allow these attributes in web.config

How to Transform .Config on build for JetBrains Rider?

I'm trying to create custom configuration depending on the environment and it seems that the best way is to use config transforms. I'm trying to have the transform happen on build (to test locally), but the changes don't seem to work. Any ideas? Also what is the correct way to have "layered config" for environment on asp.net ?
have a go at this for me...
in the web.config (base)
add this configuration under the appSettings node
..
<appSettings>
<!-- Application Settings -->
<add key="IsTest" value="true" />
then in then right click the web.config and add a transformation (Add Config Transform) if you haven't already
and in that config you will only add the transform for that case in particular
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="IsTest" value="false" xdt:Transform="Replace" xdt:Locator="Match(key)" />
...
If you are debugging from visual studio your try to keep your "Debug" values in the root config, and then lets say if you add a UAT_Release configuration and a UAT_Release transformation then when publishing your application check that the transforms have applied to your web config (these should be merged back in the base config)
for none web.config transforms (app.config for example)
these do seem to get transformed based on the build configuration
the underlying issue with web configs is that the "original" can not be preserved because the bin for www uses the .config from the project directly. it is not staged as part of the build so applying the transform would perm change the the web config.
(there was an attempt extension to solve this for VS but not seeing one for rider just yet)
my solution was to
add a local build profile that was a copy of debug just with a diff name.
crate xdt transforms for all my "develop" settings
right click preview transform and manually apply this transform to web.config (its manual but best i got so far, the perform transform does seem to fail for web.config for me or would us that)
build project using same config as applied to web and all other configs seemed to auto transform for me.

Error in Web.Config adding NotFoundMvc in ASP.Net MVC

I got this error after I added NotFoundMvc in my project. If I remove NotFoundMvc I don't get this error. What should I change in Web.Config file?
First of all check the given line is there within the top of your transform file.
<?xml version="1.0"?>
If it is there, I would also check that the following is not within your actual transformed web.config.
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
I have found the web essentials extension invaluable for resolving errors like this before I deploy.
You can simply right-click your transform file and select preview from Visual Studio and if you encounter any problems you can investigate/fix locally.
Web essentials 2012

SlowCheetah - Getting it to work

I tried using the SlowCheetah extension, but I can't seem to get it to work. I think i may have missed a step somewhere.
I downloaded the extension and installed it.
The i created my web.config file and did the "add transform"
To test it, I was already using ELmah in my project, so I tried giving it different email address to send the error log for every config, and none in the web.config.
web.config
<elmah>
<security allowRemoteAccess="0"/>
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="C:\myLogPath"/>
<errorFilter>
<test>
<or>
<equal binding="HttpStatusCode" value="404" type="Int32"/>
<is-type binding="BaseException" type="System.FieldAccessException" />
</or>
</test>
</errorFilter>
</elmah>
web.debug.config
<elmah>
<errorMail from="error-debug#domain.tld" to="me#domain.tld" priority="High" xdt:Transform="Insert"/>
</elmah>
So, when i look at "preview transform", it seems like the result is what i want. Then I start my application (either with F5 or ctrl+F5) and purposely throw an exception on my website to trigger Elmah error reporting, but I never get any email. If I add the errorMail line in my web.config, I do get an email, so the problem is not coming from Elmah.
As I said, i feel like I may have missed a simple step in setting up the extension.
The problem you are running into isn't really with SlowCheetah, but how you are launching your web application from Visual Studio.
SlowCheetah will indeed add the necessary MSBuild Targets to your project in order to do transforms upon build, but only for things included in your project output (i.e. the \bin folder).
When you build a web application your web.config stays put, and only the assemblies are copied into your \bin folder. Visual studio fires up the WebDev server and points it to the root directory of your web application. Since your web.config hasn't been modified, it will always contain the original contents.
To my knowledge, slow-Cheetah supports config transforms for the app.config files but not web.configs on debug at present. It should put a transformed web.config file in the bin folder of your project but your project still reads from the config file in the root folder. Please have a look at a workaround at http://sedodream.com/CommentView,guid,68b7e248-b9f5-4d07-bdfe-eb037bcf2cbb.aspx. This works for me instead of using Slow-Cheetah.
You can also request for web config transform support for Slow-Cheetah on debug at
https://github.com/sayedihashimi/slow-cheetah/issues/39 They are considering adding web support on F5 to it.

Environment-specific customSection properties in Web.config

I'm trying to do a bunch of environment-specific configuration in an asp.net MVC3 web application I'm building. I'm somewhat new to the platform and am running into a bit of an issue with environment specific configuration.
I have 3 configuration files:
Web.config
Web.Debug.config
Web.Release.config
I've placed my connectionString configuration in the Debug/Release files and it works, so I know asp.net recognizes at least some notion of my Debug/Release setup.
I've also added some custom configuration sections in Web.config like this:
<configSections>
<section name="GitHubConfig" type="SmartGigs.Controllers.GitHubConfig" />
<section name="JanRainConfig" type="SmartGigs.Services.JanRainConfig" />
</configSections>
Then I actually define them like this in Web.config:
<GitHubConfig ClientId="c" Secret="s" />
And attempt to load them like this:
var jrConfig = (JanRainConfig)WebConfigurationManager.GetSection("JanRainConfig");
var gitHubConfig = (GitHubConfig) WebConfigurationManager.GetSection("GitHubConfig");
Finally, I add a Debug-specific property into Web.Debug.config:
<JanRainConfig Key="key" TokenUrl="http://localhost:55739/Account/LogOn" />
The problem
If I get the property I defined in Web.config, it works perfectly. If I try to only define a property in Web.config or define it in Web.Debug.config and override it (and add xdt:Transform="Replace"), the property settings are either blank (in the former case) or contain the settings in Web.config (in the latter) - as though my configuration in Web.Debug.config is being ignored.
What is the correct way to accomplish what I'm trying to do?
Web.config transformations are only applied when publishing a project. You are certainly not the first, to get this wrong (me included). I suggest using a debug-configuration in the default web.config and apply only custom logic in the .release.config file.
It is somewhat sad, that the transformed web.config isn't picked up by VS when starting the project directly, but AFAIR the transformation process is a custom MSBuild Task, so one might be able to add execute it during compilation.

Categories

Resources