标签: webconfigurationmanager

WebConfigurationManager和ConfigurationManager之间有什么区别?

WebConfigurationManager和ConfigurationManager之间有什么区别?

我什么时候应该使用另一个?

更新

我只是查看了WebConfigurationManager,由于某种原因,您无法像在ConfigurationManager中那样访问连接字符串(如数组).谁能告诉我为什么MS这样做了?使用WebConfigurationManager获取所需的连接字符串似乎很痛苦.

再次更新CAVEAT!

如果您没有引用添加到项目中的"System.Configuration"命名空间,那么当您尝试访问WebConfigurationManager.ConnectionStrings时,Visual Studio将显示错误!

.net c# asp.net configurationmanager webconfigurationmanager

99
推荐指数
2
解决办法
3万
查看次数

如何使用物理路径打开Web配置?

我有一个在IIS7中创建网站的win表单.一个函数需要打开web.config文件并进行一些更新.(连接字符串,smtp,模拟)

但是我没有虚拟路径,只有物理路径.

有什么办法我还可以使用WebConfigurationManager吗?

我需要使用它来查找节和读/写的能力.

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration
Run Code Online (Sandbox Code Playgroud)

web-config virtual-path webconfigurationmanager c#-3.0

35
推荐指数
2
解决办法
2万
查看次数

System.Web.Configuration.WebConfigurationManager和System.Configuration.ConfigurationManager之间的行为差​​异

我在使用ASP.NET网站的测试服务器上遇到了一些麻烦.我搞砸了,并且默认网站的主目录指向了错误的位置.当我尝试:

ConfigurationManager.ConnectionStrings["connectionString"]; 
Run Code Online (Sandbox Code Playgroud)

它返回null,但是

using System.Web.Configuration;

/* ... */

var rootWebConfig =
    WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

rootWebConfig.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString;` 
Run Code Online (Sandbox Code Playgroud)

返回正确的连接字符串.

这两种方法之间有什么区别?

编辑:我真正要问的是,为什么ConfigurationManager在主目录设置不正确时方法失败,否则成功,并且WebConfigurationManager无论主目录是否正确设置都成功?是否存在其他差异,例如关于访问控制的假设?

c# asp.net configurationmanager behavior webconfigurationmanager

12
推荐指数
1
解决办法
1万
查看次数

使用带有.Net配置框架的必需子ConfigurationElement加载ConfigurationSection

我有一个控制台应用程序试图从web.config文件加载CustomConfigurationSection.

自定义配置部分具有所需的自定义配置元素.这意味着当我加载配置部分时,如果配置中不存在该配置元素,我希望看到异常.问题是.NET框架似乎完全忽略了isRequired属性.因此,当我加载配置部分时,我只创建自定义配置元素的实例并将其设置在配置部分.

我的问题是,为什么会发生这种情况?我希望GetSection()方法触发ConfigurationErrors异常,因为配置中缺少必需的元素.

以下是我的配置部分的外观.

public class MyConfigSection : ConfigurationSection
{
    [ConfigurationProperty("MyConfigElement", IsRequired = true)]
    public MyConfigElement MyElement
    {
        get { return (MyConfigElement) this["MyConfigElement"]; }
    }
}
public class MyConfigElement : ConfigurationElement
{
    [ConfigurationProperty("MyAttribute", IsRequired = true)]
    public string MyAttribute
    {
        get { return this["MyAttribute"].ToString(); }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我加载配置部分的方法.

   class Program
    {
        public static Configuration OpenConfigFile(string configPath)
        {
            var configFile = new FileInfo(configPath);
            var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
            var wcfm = new WebConfigurationFileMap();
            wcfm.VirtualDirectories.Add("/", vdm);
            return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
        } …
Run Code Online (Sandbox Code Playgroud)

c# configuration configurationsection configurationelement webconfigurationmanager

11
推荐指数
1
解决办法
4254
查看次数

有人可以提供快速的App.config/Web.config教程吗?

我之前已多次使用过这两个配置文件,但我从未花时间充分了解它们的工作原理.正如大多数人所做的那样,我理解了如何调用WebConfigurationManager.AppSettings["key"]获取配置值的基础知识.

以下是我提出的一些问题:

  1. 在类库中引用配置值时会发生什么,并且库是更大解决方案的一部分?是否需要将app.config复制到输出目录才能找到变量?(我假设是的)
  2. 你能直接在另一个类库中使用app.config的配置值吗?
  3. 假设问题3为"是",如果来自不同库的多个app.config文件包含具有相同键的配置值,会发生什么?
  4. 引用web.config但在类库中会发生什么?
  5. 当您引用app.config但在网站或Web应用程序项目中时会发生什么?

c# configurationmanager appsettings application-settings webconfigurationmanager

8
推荐指数
1
解决办法
4372
查看次数

使用 WebConfigurationManager 从 Web.config 文件中读取 appSettings 部分

我正在使用 C# 和 .NET Framework 4.7 开发 WinForm 应用程序。

我想打开一个 Web.config 文件,读取其 appSetting 部分并修改它。

要打开它,我使用这个:

 System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
Run Code Online (Sandbox Code Playgroud)

它打开它,但是当我尝试使用以下方法获取密钥时:

string[] keys = config.AppSettings.Settings.AllKeys;
Run Code Online (Sandbox Code Playgroud)

我得到一个空数组。

这是应用程序设置部分:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>

  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" /> …
Run Code Online (Sandbox Code Playgroud)

c# configurationmanager appsettings webconfigurationmanager

3
推荐指数
1
解决办法
4450
查看次数