Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

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=".logsstdout" 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_folderweb.config it's instead pointing to website_folderinDebug et461win7-x64wwwGiftOasisResponsive.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...