如何在不使用HttpContext的情况下获取Web应用程序的当前配置?

And*_*hkn 10 c# configuration web-config

我需要在我的Web应用程序中获取当前配置的实例.我发现我可以通过以下方式获得它:

Configuration configuration = WebConfigurationManager.OpenWebConfiguration( HttpContext.Current.Request.ApplicationPath);
Run Code Online (Sandbox Code Playgroud)

但是在我的项目的某些部分,我没有HttpContext,所以我需要在不使用HttpContext的情况下获取配置的实例.

Seb*_*ldi 11

你正确的Lubochkn~诀窍......

_webConfigurationManager = WebConfigurationManager.OpenWebConfiguration("~");
Run Code Online (Sandbox Code Playgroud)


Jam*_*son 1

你也应该能够得到web.config这样的结果:

Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
Run Code Online (Sandbox Code Playgroud)

  • 我用“~”替换“/”,它就工作了!`配置 config = WebConfigurationManager.OpenWebConfiguration("~");` (19认同)