Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Web.config settings in Asp.Net Core App?

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.

like image 402
RonC Avatar asked Oct 21 '16 22:10

RonC


2 Answers

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.

like image 60
RonC Avatar answered Sep 21 '22 18:09

RonC


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> 
like image 25
Yush0 Avatar answered Sep 21 '22 18:09

Yush0