【问题标题】:How can I get current config of the web application without using HttpContext?如何在不使用 HttpContext 的情况下获取 Web 应用程序的当前配置?
【发布时间】:2011-12-24 13:24:21
【问题描述】:

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

Configuration configuration = WebConfigurationManager.OpenWebConfiguration( HttpContext.Current.Request.ApplicationPath);

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

【问题讨论】:

标签: c# configuration web-config


【解决方案1】:

你是对的 Lubochkn ~ 成功了...

_webConfigurationManager = WebConfigurationManager.OpenWebConfiguration("~");

【讨论】:

    【解决方案2】:

    你应该也可以像这样得到web.config

    Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
    

    【讨论】:

    • 这不起作用,这段代码在%systemdisk%/inetpub/wwwroot/web.config 中打开配置,它不是我需要的配置。当我使用参数HttpContext.Current.Request.ApplicationPath 执行此操作时,它会返回另一个 web.config。
    • 我将“/”替换为“~”,这样就可以了! Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    • Yes "/" 获取根 web.config,而不是当前应用程序的 web.config,而 "~" 似乎可以工作。
    猜你喜欢
    • 2011-01-25
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    相关资源
    最近更新 更多