我试图找出如何使用Linq从我的web.config文件中过滤掉一些我的appsettings.
我正在尝试执行以下操作(语法错误): -
var query = from q in System.Web.Configuration.WebConfigurationManager.AppSettings.Keys
where q.StartsWith("Foo")
select q);
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
编辑:添加screenie(这是一个链接)
如何从App.Config获取值.
码:
<configuration>
<appSettings>
<add key="ShowRoomCode" value="1000"/>
<add key="FolderPath" value="D:\\Images\\Book\\"/>
</appSettings>
</configuration>
string imageFolderPath = ConfigurationManager.AppSettings["FolderPath"];
Run Code Online (Sandbox Code Playgroud)
但它返回null值.配置文件位于同一项目中.
最近,我发现"Web.Config"文件包含一个<appSettings>看起来适合存储应用程序设置的部分.哎呀,它甚至有一种通过标准系统库访问文件的编程方式.所以聪明一点,我写了一个接口来访问它,然后是接口的具体实现,如下所示:
public interface IAppSettings
{
IEnumerable<string> GetValues(string componentName, string settingName);
IEnumerable<KeyValuePair<string, string>> GetValuePairs(string componentName, string settingName);
void SetValues(string componentName, string settingName, IEnumerable<string> valueList, bool append);
void SetValuePairs(string componentName, string settingName, IEnumerable<KeyValuePair<string, string>> pairList, bool append);
}
Run Code Online (Sandbox Code Playgroud)
然后我发现在应用程序运行时将设置保存回"web.config"会导致整个应用程序重新启动.这对我来说似乎是完全不合理的,因为如果我回写web.config并且应用程序每次都重新启动,那么像HttpRuntime.Cache这样的东西会被完全清空,这会使我的Cache无用,因为它会不断地清空和重新填充.
所以我想知道:我应该在哪里存储我的应用程序设置?
有没有一个很好的解决方案,所以我不必自己滚?
编辑:
好的,感谢所有建议使用数据库和潜在表模式的人.我想我将采用以下架构:
settings:
index NUMBER NOT NULL AUTO_INCREMENT <== Primary Key
component NVARCHAR(255) NOT NULL
setting NVARCHAR(255) NOT NULL
key NVARCHAR(255)
value NVARCHAR(255) NOT NULL
Run Code Online (Sandbox Code Playgroud)
虽然我不认为我会"设置"P-Key,但使用Auto-Incr索引代替.这样,如果我有一个需要将内容邮寄到多个管理器的应用程序,我可以存储许多:
index component setting value
1 RequestModule ManagerEmail manager1@someplace
2 RequestModule ManagerEmail …Run Code Online (Sandbox Code Playgroud) 有谁知道是否有办法了解加载app.config文件的过程?
我遇到过很多情况,在这个过程中出现问题(通常是配置文件中的错误值),但是需要大量的猜测或消除过程才能找到问题.
例如,以下某些方面给我带来了麻烦:
useLegacyV2RuntimeActivationPolicy和相关节点)这可能是一厢情愿的想法,但如果有一些秘密日志可以启用,或者某种方式迫使运行时在加载配置系统时提供更多信息,那将会很棒.
我试图从我的app.config读取设置,我确定它以前工作,但现在它返回nullReferenceException.
获取设置的代码如下:
codeValueUtilRx = ConfigurationManager.AppSettings["CODEVALUE_UTIL_RX"].Split(';').ToList();
Run Code Online (Sandbox Code Playgroud)
我的app-congfig如下:
<appSettings>
<add key ="LOGFILELOCATION" value ="C:\\RuleEditor\\"/>
<add key ="CODEVALUE_UTIL_RX" value="GCN;GRP;NDC;SPEC;TCC"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我感觉它看起来很明显,我只是想不通.我已经尝试将app-config移动到解决方案中的不同项目,并且我重新创建了文件,但没有运气.有任何想法吗?
是否可以从应用程序本身更改网站的应用程序设置?
这不是日常操作,而是自助服务重新配置选项.非开发人员可以更改特定设置,这应该会导致重新启动,就像我可以在网站配置页面上手动执行(应用程序设置部分)
假设我使用新的DepencyInjection框架在新的ASP.Net/vNext中配置我的类和依赖项.
我该如何使用,如何获取预定义的配置设置?
public void ConfigureServices(IServiceCollection services)
{
// Add Application settings to the services container.
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
// Add EF services to the services container.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Configure the options for the authentication middleware.
// You can add options for Google, Twitter and other middleware as shown below.
// For more information see http://go.microsoft.com/fwlink/?LinkID=532715
services.Configure<FacebookAuthenticationOptions>(options =>
{
options.AppId = Configuration["Authentication:Facebook:AppId"];
options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
});
services.Configure<MicrosoftAccountAuthenticationOptions>(options …Run Code Online (Sandbox Code Playgroud) 试图简化<appSettings>dev与prod.
我的Web.config:
<appSettings>
<add key="hello" value="debug" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我的Web.Release.config:
<appSettings>
<add key="hello" value="prod" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
(均在<configuration>)
当我在调试模式下运行并运行我的MVC站点时,我可以return Content(WebConfigurationManager.AppSettings["hello"]);在我的内容中执行简单操作HomeController.Index并返回dev.如果我将模式切换到Release它仍然返回dev.我想模拟prod模式而不实际发布到prod.
下面是我面临的 Key-Vault 问题。 \n\xc2\xa0\n我有一个 Key-Vault,它存储一些在 Web 应用程序中使用的密钥,并使用 \n@Microsoft.KeyVault(SecretUri=)\ n\xc2\xa0\n秘密的值将使用自动化每三天更改一次。这也会更改版本号 (GUID),然后应用程序设置值(在“主要”和“辅助”情况下至少有一个)将变得无效。\n有什么方法可以在应用程序设置中引用 KeyVault 中的最新值吗?\更新所有网站中的应用程序设置将是一个乏味的过程。
\n我有一个 .NET Core WebApi 项目。为了以简单的方式读取应用设置,我配置了要注入 DI 的选项。这工作正常。但是,如果我尝试Configure<>()使用泛型类型参数进行调用,则会出现错误。
参数 2:无法从“Microsoft.Extensions.Configuration.IConfigurationSection”转换为“System.Action”
除了方法重载Configure<T>(Action<T> configureOptions)似乎不再可用。
为什么调用不适用于泛型类型参数?
启动文件
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
// services.AddMvc() etc...
services.AddOptions();
// Works fine
services.Configure<MyOption>(Configuration.GetSection(nameof(MyOption)));
}
private void AddOption<T>(IServiceCollection services)
{
// Wont work
services.Configure<T>(Configuration.GetSection(nameof(T)));
services.Configure<T>(Configuration.GetSection(""));
}
}
Run Code Online (Sandbox Code Playgroud)
MyOption.cs
public class MyOption
{
public bool MyProp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
appsettings.json
{
"MyOption": {
"MyProp": true …Run Code Online (Sandbox Code Playgroud) c# dependency-injection appsettings .net-core asp.net-core-webapi
appsettings ×10
c# ×5
app-config ×2
asp.net ×2
.net ×1
.net-core ×1
azure ×1
debugging ×1
linq ×1
web-config ×1
wpf ×1