50

I understand that asp.net core has a new configuration system that is quite flexible and that's great. But there are things I like about the web.config based configuration system from .net 4.x. For example one can put comments in the web.config file since it's an xml file. And that for me is worth sticking with xml rather than going with the shiny new json approach. [Update: I now understand that the json approach also supports comments in the file.]

So, if I have a Asp.Net Core Web Project that targets the full framework it seems like I should be able to use the web.config based System.Configuration.ConfigurationManager.AppSettings[key] approach to getting a setting.

But when I try, the value always comes back null (at least with IIS express using VS2015).

It should work right? Any thoughts on what I might be over looking?

Web.config

<configuration>
    <appSettings>
        <add key="SomeSetting" value="true"/>
    </appSettings>

    <system.webServer>
        <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
        </handlers>

        <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
     </system.webServer>
</configuration>

Code to access setting:

string key = "SomeSetting";
string setting = ConfigurationManager.AppSettings[key];
if (setting == null)
       throw new Exception("The required configuration key " + key + " is missing. ");

UPDATE
After more research I now understand why it doesn't work but I still haven't found a way to fix it. The root cause seems to be that the ConfigurationManager is looking for the config information in a different file and not in the web.config.

This can be seen by looking at AppDomain.CurrentDomain.SetupInformation.ConfigurationFile property. In my case instead of pointing to the website_folder\web.config it's instead pointing to website_folder\bin\Debug\net461\win7-x64\wwwGiftOasisResponsive.exe.Config where website_folder is the path to the folder containing my website.

The documentation and intellisense say AppDomain.CurrentDomain.SetupInformation.ConfigurationFile is a settable property but when I try I find that setting it does not change it's value. Very odd.

So while I now see what the issue is I can't seem to find a way to fix it.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
RonC
  • 31,330
  • 19
  • 94
  • 139
  • 2
    You can have a .xml instead of a .json and use the XmlConfigurationProvider – tmg Oct 22 '16 at 15:35
  • 1
    Maybe I am wrong, but I get the impression that you think that it is not possible to comment in json configuration files. Microsoft.Extensions.Configuration.Json supports comments. –  Oct 22 '16 at 17:03
  • @tmg- thanks, that's a good fallback plan I'll look into. Still, I don't understand why ConfigurationManager can't be used to get values from the web.config... – RonC Oct 23 '16 at 21:23
  • @RandyVanElburg- you are not wrong, my understanding was that xml doesn't support comments. Can you provide a link to an an article or other documentation that shows that it supports comments in the xml file? – RonC Oct 23 '16 at 21:29
  • 1
    I cannot find it the documentation, but in this issue (https://github.com/aspnet/Configuration/issues/87), the last comment says: "Right now both single line and multiline comments work on json files." And take a look at the test here: https://github.com/aspnet/Configuration/blob/release/test/Microsoft.Extensions.Configuration.Json.Test/JsonConfigurationTest.cs Look for 'SupportAndIgnoreComments' at line 65. –  Oct 24 '16 at 08:14
  • 1
    @RandyVanElburg - I have verified that appsettings.json does in fact support comments. I _really_ appreciate you pointing this to me. In the end I'm going to leverage the new .net core config system due to that insight. Thank you. – RonC Oct 25 '16 at 18:58
  • Any idea how to do the reverse? I'm accessing a non core class library from a core mvc 2 app. How can I pass through the web.config connection string? – niico May 24 '18 at 01:59
  • That is best asked as it’s own separate question on SO. – RonC May 24 '18 at 03:12

3 Answers3

47

I kinda found the solution. The key to figuring it out was realizing that the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile property wasn't pointing to the web.config file but rather to the exe.config file for the executable running the website. Remember, under .net core, the website runs in its own process and it has its own exe.

So the config model that .Net 4.x uses with the ConfigurationManager is more like that of a desktop app than a 4.x web application. By that I mean that it's looking at the exe.config not the web.config.

Then I noticed that the Asp.Net Core Web Project (using the full framework) contains an app.config file much like a desktop app would. And it turns out that if you put your .net 4.x application config settings in that file they will get placed in the exe.config file when the exe is generated, whether for debug or for release. Just exactly like it works with a win forms app for example.

So the way to utilize the ConfigurationManager in an asp.net core web application that targets the full framework is to put the application setting in the app.config file rather than the web.config file. The ConfigurationManager will find them no problem.

enter image description here

While this explains a lot, it still doesn't provide that ability to actually put those settings in the web.config and access them via the ConfigurationManager. But I'm beginning to believe that's not possible in a asp.net core web application even if it is targeting the full framework.

Appulus
  • 18,630
  • 11
  • 38
  • 46
RonC
  • 31,330
  • 19
  • 94
  • 139
  • 5
    I have been banging my head against a wall trying to figure this out. The separation of the new Core and full .NET Framework is so hazy, I never feel like I know, which I'm working with, and how they work. – Dynde Jan 16 '17 at 11:34
  • I hear ya. Looking at the namespaces can help. i.e. stuff in the Microsoft.AspNetCore namespace is part of core. If you are targeting the full framework the main rule is to stay away from the System.Web.HttpContext object and all sub objects (it's request, response, cookies, etc objects) – RonC Jan 16 '17 at 13:39
  • but that's part of the confusion - I STARTED a new solution targeting the full .NET, but I got a project.json with aspnetcore dependencies. And I got a web.config with configuration as well (and I keep reading that appsettings.json is the new way of doing things). It's all a jumbled mess. – Dynde Jan 17 '17 at 15:05
  • 2
    The project.json is there because version VS2015 tooling uses project.json as it's project file, the web.config is there because IIS still uses it for configuration even though Asp.Net Core doesn't per se. However, Asp.Net Core needs it to configure the proper IIS Module for running Asp.Net Core. In Asp.Net core, appsettings.json is where the application config info is stored rather than in web.config. It can seem like a jumbled mess but really it's just a matter of getting familiar with what each piece is used for now. Hope that helps. – RonC Jan 17 '17 at 15:13
  • 1
    Oh, I understand why the pieces are all there, but I don't understand why, when I started a full .NET solution, it uses SOME of the 'old' .NET conventions, and SOME of the new Asp.Net Core conventions. I didn't get a appsettings.json for instance. – Dynde Jan 18 '17 at 13:15
  • So far, I have not been able to have one methodology for both environments, you need to use the json approach in Core en the app.config approach in .NET framework. My issue with this is even bigger than having two approaches: the app.config gets distributed with every deployment. You can't have different app.config files per environment, or at least I have not found how. – Pieter van Kampen May 08 '17 at 20:51
  • I manage my app.configs manually by but you can get visual studio to help. In such a case you can have App.Debug.config and App.Release.config. For example see: https://mitasoft.wordpress.com/2011/09/28/multipleappconfig/ – RonC May 08 '17 at 20:54
  • I wonder how `AppDomain.CurrentDomain.SetupInformation` can point anywhere if it just doesn't exist in .net core? – Shrike Aug 21 '17 at 15:22
  • @Shrike -`AppDomain.CurrentDomain.SetupInformation.ConfigurationFile` does exist if the Asp.Net Core project is targeting the full framework. I just doubled checked it in the debugger. – RonC Aug 21 '17 at 15:27
  • Ah, you meant .net framework, then sure it does. It has nothing to do with asp.net. It's part of .NET. But in .NET Core it's absent. – Shrike Aug 21 '17 at 15:31
  • 1
    This solution worked for me. I moved the settings over to the app.config file and now it works. Thanks! – Andrew Nov 04 '20 at 19:29
7

I ran into this problem when I started publishing my asp.net core 1.1 application to IIS.
There would be generated web.config file on the IIS which was overriten on publishing. To enable Windows Authentification I had to add a web.config manually to my Project. This one gets published correctly to IIS:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\yourproject.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    <security>
      <authentication>
        <anonymousAuthentication enabled="false" />
        <windowsAuthentication enabled="true" />
      </authentication>
    </security>
  </system.webServer>
</configuration>
Yush0
  • 1,547
  • 18
  • 22
4

I also ran into this issue. After some investigation and reading it's like that you are able to add web.config manually but this is for providing settings for IIS (e.g Authentication, ...).

For the appsettings or custom settings you have to work with the appsettings.json file and the new Configuration in .Net Core.

Microsoft Documentation

jawa
  • 206
  • 2
  • 10