如果我使用像这样的app.config,通过继承自System.Configuration.Section的类来获取"页面"列表的正确方法是什么?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="XrbSettings" type="Xrb.UI.XrbSettings,Xrb.UI" />
</configSections>
<XrbSettings>
<pages>
<add title="Google" url="http://www.google.com" />
<add title="Yahoo" url="http://www.yahoo.com" />
</pages>
</XrbSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud) 我正在使用log4net进行日志记录.我的日志配置存储在单独的文件中.
Web.Config中:ConfigSections
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
Run Code Online (Sandbox Code Playgroud)
在AssemblyInfo.cs中指定我的配置文件
[assembly: log4net.Config.XmlConfigurator(ConfigFile="Log4Net.config", Watch = true)]
Run Code Online (Sandbox Code Playgroud)
当我初始化我的LogManager时,我收到此错误
"System.TypeLoadException"
message: Could not load type 'log4net.Config.Log4NetConfigurationSectionHlandler' from assembly 'Log4net'.
Run Code Online (Sandbox Code Playgroud)
是的它说"Log4NetConfigurationSectionHlandler",它不是一个错字
后来,这个错误
An error occurred creating the configuration section handler for log4net: Could not load type 'log4net.Config.Log4NetConfigurationSectionHlandler' from assembly 'Log4net'.
Run Code Online (Sandbox Code Playgroud)
编辑:试过Mauricio Scheffer的建议
拿到
log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
Run Code Online (Sandbox Code Playgroud) 我卡住了!这看起来真的很愚蠢,但我看不出我错在哪里.我正在创建一个2.0 C#ASP.NET网站.我试图在web.config文件中使用自定义部分:
DatabaseFactorySectionHandler sectionHandler = ConfigurationManager.GetSection("DatabaseFactoryConfiguration") as DatabaseFactorySectionHandler;
Run Code Online (Sandbox Code Playgroud)
我有一个单独的DLL用于Bailey.DataLayer命名空间中的对象.但是当我运行test.aspx页面时,我收到以下错误:
System.Configuration.ConfigurationErrorsException was unhandled by user code
Message="An error occurred creating the configuration section handler for DatabaseFactoryConfiguration: Could not load file or assembly 'Bailey.DataLayer' or one of its dependencies. The system cannot find the file specified. (C:\\Documents and Settings\\Administrator.PIP\\My Documents\\Visual Studio 2005\\WebSites\\bailey\\web.config line 13)"
Source="System.Configuration"
Run Code Online (Sandbox Code Playgroud)
我想要获得的课程如下:
namespace Bailey.DataLayer
{
public sealed class DatabaseFactorySectionHandler : ConfigurationSection
{
[ConfigurationProperty("Name")]
public string Name
{
get { return (string)base["Name"]; }
}
[ConfigurationProperty("ConnectionStringName")]
public string ConnectionStringName
{ …Run Code Online (Sandbox Code Playgroud) 我正在编写一个可以使用几个不同主题的页面,我将在web.config中存储有关每个主题的一些信息.
是否更有效地创建一个新的sectionGroup并将所有内容存储在一起,或者只是将所有内容放在appSettings中?
configSection解决方案
<configSections>
<sectionGroup name="SchedulerPage">
<section name="Providers" type="System.Configuration.NameValueSectionHandler"/>
<section name="Themes" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<SchedulerPage>
<Themes>
<add key="PI" value="PISchedulerForm"/>
<add key="UB" value="UBSchedulerForm"/>
</Themes>
</SchedulerPage>
Run Code Online (Sandbox Code Playgroud)
要访问configSection中的值,我使用以下代码:
NameValueCollection themes = ConfigurationManager.GetSection("SchedulerPage/Themes") as NameValueCollection;
String SchedulerTheme = themes["UB"];
Run Code Online (Sandbox Code Playgroud)
appSettings解决方案
<appSettings>
<add key="PITheme" value="PISchedulerForm"/>
<add key="UBTheme" value="UBSchedulerForm"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
要在appSettings中访问值,我正在使用此代码
String SchedulerTheme = ConfigurationManager.AppSettings["UBSchedulerForm"].ToString();
Run Code Online (Sandbox Code Playgroud) 在.NET中,我们可以使用<configSections>元素创建自定义配置节,如下所示:
<configuration>
<configSections>
<section name="dictionarySample"
type="System.Configuration.DictionarySectionHandler"/>
<section name="nameValueSample"
type="System.Configuration.NameValueSectionHandler" />
</configSections>
<dictionarySample>
<add key="key1"
value="value1"/>
</dictionarySample>
<nameValueSample>
<add key="key2"
value="value2" />
</nameValueSample>
</configuration>
Run Code Online (Sandbox Code Playgroud)
上面,我定义了两个部分.一种类型DictionarySectionHandler,另一种类型NameValueSectionHandler.
据我所知,这两个处理程序以完全相同的方式使用,并导致相同的配置部分.
那么,是否存在差异,或者我可以互换使用它们吗?
我读了这里已经发布的所有答案,但似乎没有解决.即使我收到警告,Elmah也能完美地工作.当我在visual studio 2012中启动debug(f5)时,我也遇到了这些错误.在构建时一切都很好.
web.config中的配置部分很简单,所以我真的不知道如何解决它:
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
</configSections>
<elmah>
<security allowRemoteAccess="1" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="EvaConnection" />
<!--<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/ErrorLogs" />-->
<!-- ELMAH class ErrorMailModule.cs has been modified to read config from AppSettings -->
<errorMail from="dummy@dummy.com" to="dummy@dummy.com" subject="dummy" priority="High" async="true" smtpPort="25" smtpServer="dummy" useSsl="false" userName="dummy@dummy.com" password="dummy" noYsod="false" />
</elmah>
Run Code Online (Sandbox Code Playgroud) 我现在可能完全偏离轨道,所以我会在这里问这个,所以有人可以帮助我.
我想要做的是将存储在applicationSettings区域的web.config中的值插入到我的aspx标记中.具体来说,我想从配置中恢复URL.这是我使用的configSection设置
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=123456">
<section name="MyApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=12345" requirePermission="false" />
</configSections>
Run Code Online (Sandbox Code Playgroud)
稍后在该文件中的实际设置如下:
<applicationSettings>
<MyApp.Properties.Settings>
<setting name="ImagesUrl" serializeAs="String">
<value>http://resources/images/</value>
</setting>
Run Code Online (Sandbox Code Playgroud)
现在我想在标记中引用上面的值,如下所示:
<asp:Image ID="Image1" runat="server" ImageUrl="<%$AppSettings:ImagesUrl%>/Image1.jpg
Run Code Online (Sandbox Code Playgroud)
我知道有一个表达式<%$ AppSettings:ImagesUrl%>,但我没有使用web.config的appsettings部分,而是使用configSection.
编辑:我相信我只能用ExpressionBuilder来做,因为我必须将字符串与单个图像名称连接起来.我改变了上面的例子来反映这一点.
我喜欢下面的Bert Smith Code Solution来访问配置部分,只需要将它放在表达式构建器中.我坚持要从我调用配置管理器的地方覆盖GetCodeExpression方法,但我不明白如何构建表达式参数.
public class SettingsExpressionBuilder: ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
return ??
}
Run Code Online (Sandbox Code Playgroud)
编辑
结果如下所示,适用于各种文件,而不仅仅是图像:
<asp:ScriptReference Path='<%$Code:GetAppSetting("ResourcesUrl","JS/jquery/jquery.jqplot.js")%>'
Run Code Online (Sandbox Code Playgroud)
我只是使用Microsoft的示例从表达式构建器返回任何类型的代码:
返回新的CodeSnippetExpression(entry.Expression);
GetAppSetting是我的自定义Page类中的一种方法.
当我尝试使用时检索.config文件中的部分列表
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Run Code Online (Sandbox Code Playgroud)
config.Sections集合包含一堆系统部分,但我没有在configSections标记中定义文件的部分.
在一个小型的c#项目中,我正在尝试创建一个简单的自定义配置部分.我按照CodeProject中的说明进行操作:解开.NET 2.0配置之谜,一切都运行得很好......除了我没有在配置上获得xsd验证和智能感知这一事实.
我的配置如下所示.
<configuration>
<configSections>
<section name="pizza" type="TestConfig.Configuration.PizzaConfigurationSection, TestConfig.Configuration"/>
</configSections>
<pizza name="Margherita" timeToCook="00:10:00" price="15.12">
<cook firstName="Nicola" lastName="Carrer" rank="7" />
<toppings>
<add name="Mozzarella" percentage="0.6" />
<add name="Tomato sauce" percentage="0.28" />
<add name="Oregano" percentage="0.02" />
<add name="Mushrooms" percentage="0.1" />
</toppings>
</pizza>
</configuration>
Run Code Online (Sandbox Code Playgroud)
在本文(XSDExtractor)上,我找到了一个为configsection创建xsd文件的工具.它工作正常,即它为主要属性(例如"价格")和单个元素("烹饪")提供智能感知和验证.但是我无法使其适用于收藏品("浇头").
我的问题:
非常感谢,尼古拉
我想从app.config中阅读以下自定义部分:
<StartupFolders>
<Folders name="a">
<add folderType="Inst" path="c:\foo" />
<add folderType="Prof" path="C:\foo1" />
</Folders>
<Folders name="b">
<add folderType="Inst" path="c:\foo" />
<add folderType="Prof" path="C:\foo1" />
</Folders>
</StartupFolders>
Run Code Online (Sandbox Code Playgroud)
我找到了此链接,但没有为多个<Folders>标签说。请帮忙?
我正在研究如何在app.config的configsection中创建节组和节。所以,让我们取样
<configSections>
<sectionGroup name="trackGroup">
<section name="trackInfo" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<trackGroup>
<trackInfo>
<add key = "ASHUM" value="ASH-UM"/>
</trackInfo>
</trackGroup>
Run Code Online (Sandbox Code Playgroud)
因此,当我浏览各种文章时,我发现每个人的节使用不同的类型。因此,我在这里发现了不同的类型:http : //msdn.microsoft.com/zh-cn/library/aa309408%28v=vs.71 %29.aspx
但是这里没有提到我使用的类型。我从一个随机示例中获得了这种类型,据我了解,它实际上是在为appsetting部分定义设置。有人可以帮助我,我的样本中的类型是什么意思,我的意思是版本,公共令牌,文化如何定义这些内容?我也想知道哪种类型更好用?就像我必须在运行时访问这些设置,甚至在运行时进行一些修改。
另外我想这些不同的类型有不同的访问方式?像上面示例中的情况一样,我正在按以下方式访问键和值:
static void getFull(string sectionName)
{
System.Collections.Specialized.NameValueCollection section = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection(sectionName);
if (section != null)
{
foreach (string key in section.AllKeys)
{
Console.WriteLine(key + ": " + section[key]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我们使用MSDN链接中提供的类型,那么我将如何访问键和值?
我正在尝试使用Powershell在web.config中sectionGroup向configuration/configSections元素添加元素.
我现在有
$filePath = [path to my web.config file]
# load the XML from the web.config
$xml = New-Object XML
$xml = [xml](Get-Content $filePath)
# navigate to the <configSections> element
$xmlConfigSections = $xml.SelectSingleNode("//configuration/configSections")
# create the new <sectionGroup> element with a 'name' attribute
$sectionGroup = $xml.CreateElement("sectionGroup")
$xmlAttr = $xml.CreateAttribute("name")
$xmlAttr.Value = "myCustomSectionGroup"
$sectionGroup.Attributes.Append($xmlAttr)
# now add the new <sectionGroup> element to the <configSections> element
$xmlConfigSections.AppendChild($sectionGroup)
#save the web.config
$xml.Save($filePath)
Run Code Online (Sandbox Code Playgroud)
但这会导致该CreateElement方法出现异常:
"指定的节点不能作为此节点的有效子节点插入,因为指定的节点类型错误."
我不明白为什么当我尝试创建元素时抛出这样的异常(异常似乎与附加元素有关). …
configsection ×12
c# ×6
web-config ×5
app-config ×4
asp.net ×3
.net ×2
appsettings ×2
elmah ×1
log4net ×1
logging ×1
markup ×1
performance ×1
powershell ×1