我正在编写一个可以使用几个不同主题的页面,我将在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) 有没有一种方法可以从<ApplicationSettings>NLog布局变量中的web.config部分获取值?
我已经在我的web.config中存储了SMTP详细信息,并且不希望复制这些设置只是为了在我的NLog.config中使用.
理想情况下,我想做类似的事情:${aspnet-config:SmtpHostServer}
然后从web.config中获取值
NLog.config文件未设置连接字符串.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- Load the ASP.NET Core plugin -->
<extensions>
<add assembly="NLog.Web.AspNetCore" />
</extensions>
<variable name="SirNLogDb" value="data source=SQL_MULALLEY;initial catalog=LogFiles;User ID=xxx;Password=yyy;">
</variable>
<!-- providerName="System.Data.SqlClient"-->
<!-- the targets to write to -->
<targets>
<target name="db"
xsi:type="Database"
dbProvider="System.Data.SqlClient"
connectionString="${var:SirNLogDb}"
commandType="StoredProcedure"
commandText="[dbo].[NLog_AddEntry_p]">
<parameter name="@machineName" layout="${machinename}" />
<parameter name="@siteName" layout="${iis-site-name}" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@username" layout="${aspnet-user-identity}" />
<parameter name="@message" layout="${message}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@properties" layout="${all-event-properties:separator=|}" />
<parameter …Run Code Online (Sandbox Code Playgroud) 我正在使用C#.NET 2.0 Windows应用程序.
我正在使用app.config进行应用程序设置.
但AppSettings中的更改并未反映运行时,需要重新启动应用程序.
我怎么能避免它.
这是我用来读取和编写应用程序设置的代码片段.
我正在读这样的设置
string temp = ConfigurationManager.AppSettings.Get(key);
Run Code Online (Sandbox Code Playgroud)
我正在更新这样的值,其中node是当前配置/ appSettings节点
node.Attributes["value"].Value = value;
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Run Code Online (Sandbox Code Playgroud) 我真的不知道为什么我会对此有所了解0:

但是这段代码效果很好:
int val = Convert.ToInt32("1546");
Run Code Online (Sandbox Code Playgroud)
这是appsetting:
<add key="PesoPayMerchantId" value="?1546"/>
Run Code Online (Sandbox Code Playgroud)
任何的想法?
EDIT1
我想得到整数值"1546",但它无法正常工作.
以下是获取appsetting的代码:
public static string GetConfigurationString(string appSettingValue)
{
return ConfigurationManager.AppSettings[appSettingValue];
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了你的建议,这就是结果:



字符串值是正确的("1546"),但不能解析为整数.这里发生了什么?
编辑2
我非常肯定价值:
<add key="PesoPayMerchantId" value="?1546"/>
Run Code Online (Sandbox Code Playgroud)
实际上是数字的组合"1546"
但是当我尝试使用Immediate Window它重新编写字符串值时,现在可以解析它.但我仍然无法弄清楚这个的原因是Bug什么?

编辑3
最后,感谢Johnny,它现在有效
我做的是,我重写了整个,<add key="PesoPayMerchantId" value="1546"/>现在可以解析了.感谢你的帮助.:d
我在web.config中有很多appSettings经常使用(即在每个帖子后面).是否ConfigurationManager中持有过程中的这些值或在那里复制这些值转换成应用程序状态上ApplicationStart(),然后从那里检索它们可以有任何的性能提升?
我需要使用App.config文件获取" http://example.com ".
但目前我正在使用:
string peopleXMLPath = ConfigurationManager.AppSettings["server"];
Run Code Online (Sandbox Code Playgroud)
我无法获得价值.
你能指出我做错了什么吗?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<section name="device" type="System.Configuration.SingleTagSectionHandler" />
<section name="server" type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<device id="1" description="petras room" location="" mall="" />
<server url="http://example.com" />
</configuration>
Run Code Online (Sandbox Code Playgroud) 我在wpf项目中有一个配置文件来存储connectionstring.但是当我尝试获取AppSettings和ConnectionStrings时,我得到null.
WEB.config文件是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Trackboard" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf"/>
</connectionStrings>
<appSettings>
<add key="Trackboard" value="Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我尝试过几种方式:
W1: ConnStr = ConfigurationManager.ConnectionStrings["Trackboard"].ConnectionString;
W2: ConnStr = ConfigurationManager.ConnectionStrings[0].ConnectionString;
W3: ConnStr = ConfigurationManager.AppSettings["Trackboard"];
W4: ConnStr = ConfigurationManager.AppSettings[0];
Run Code Online (Sandbox Code Playgroud)
他们都没有工作.
但这个工作:
ConnStr = @"Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf";
Run Code Online (Sandbox Code Playgroud)
(这意味着我不能使用配置文件,这违背我的意愿)我需要帮助.
这看起来应该很简单,我一直在搜索 SO 和许多其他地方以寻找答案,我发现并尝试过的一切都不起作用。
我有一个看起来像这样的 appsettings.json 文件
"Email": {
"Port": "25",
"Host": "localhost",
"EnableSSL": "false",
"Credentials": {
"Username": "fakeuser",
"Password": "fakepassword"
},
"SystemFromAddress": "testsender@localhost.com",
"SystemFromDisplayName": "Test Sender",
"EmailTemplateRootDirectory": "Email\\EmailTemplates",
"EmailTemplates": [
{
"TemplateKey": "ResetPassword",
"TemplatePath": "ResetPassword.cshtml"
},
{
"TemplateKey": "NewAccount",
"TemplatePath": "NewAccount.cshtml"
},
{
"TemplateKey": "VerifyEmail",
"TemplatePath": "VerifyEmail.cshtml"
}
]
Run Code Online (Sandbox Code Playgroud)
}
我正在尝试绑定多个模型(EmailOptions 是父模型),EmailOptions 类期望从 appsettings.json 中的 EmailTemplates 列表填充它的 EmailTemplates 列表,如上所示。
appsettings.json 文件按预期填充父类,此类中的电子邮件模板子列表始终为空。
这是我绑定到的类。
public class EmailOptions
{
public int Port { get; set; }
public string Host { get; set; } …Run Code Online (Sandbox Code Playgroud) 我创建了一个 .net core 辅助服务,并将 IConfiguration 注入其中,以便我可以从 appsettings.json 读取属性,但每次运行该文件时,该属性都会显示为 null。但是,如果我强制 IConfiguration 通过 ConfigurationBuilder 添加 json 文件,它就可以工作。如何使用默认设置来允许 .net core 辅助服务从 appsettings 读取?
public class ImageFileWatcher : IHostedService, IDisposable
{
private readonly ILogger _logger;
private readonly IConfiguration _configuration;
FileSystemWatcher _watcher;
public ImageFileWatcher(ILogger<ImageFileWatcher> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
// IConfigurationBuilder builder = new ConfigurationBuilder()
// .AddJsonFile("appsettings.json")
// .AddEnvironmentVariables();
// this._configuration = builder.Build();
}
public Task StartAsync(CancellationToken cancellationToken)
{
var watcherConfig = _configuration["WatcherConfig:filePath"];//this is where it comes up as null. …Run Code Online (Sandbox Code Playgroud) appsettings ×10
c# ×7
web-config ×3
.net ×2
asp.net ×2
asp.net-core ×2
nlog ×2
wpf ×2
.net-core ×1
integer ×1
performance ×1
sql ×1
string ×1