我有两个关于在web.config中保存设置的方法的问题.
Appsettings:查看web.config
<appSettings>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
代码隐藏中的用法:
ConfigurationManager.AppSettings["key1"];
Run Code Online (Sandbox Code Playgroud)
ApplicationSettings/Properties(使用项目中的'properties'选项卡自动生成)
查看web.config
<applicationSettings>
<Projectname.Properties.Settings>
<setting name="TestEnvironment" serializeAs="String">
<value>True</value>
</setting>
</Projectname.Properties.Settings>
</applicationSettings>
Run Code Online (Sandbox Code Playgroud)
代码隐藏中的用法:
Properties.Settings.Default.TestEnvironment
Run Code Online (Sandbox Code Playgroud)
那么,web.config中设置的这两种存储可能性有什么区别?
据我所知,appSettings的一个缺点是你自己修改了web.config并且appSettings不是强类型的,就像applicationSettings一样.
两者都可以在Web部署项目中替换.
就我而言,appSettings没有用处.我在这里错过了什么吗?哪个是历史悠久的旧版?
我必须在iPhone设置中获取当前国家/地区.谁能告诉我如何在iPhone应用程序中获取当前国家/地区.
我必须使用当前国家来解析我需要通过当前国家/地区的RSS源.
请帮我找到这个国家.
提前致谢.
我在NET Core2.0 App中有以下类.
// required when local database does not exist or was deleted
public class ToDoContextFactory : IDesignTimeDbContextFactory<AppContext>
{
public AppContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<AppContext>();
builder.UseSqlServer("Server=localhost;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true");
return new AppContext(builder.Options);
}
}
Run Code Online (Sandbox Code Playgroud)
当Database不存在时,在Core 2.0中需要进行迁移,并且必须在运行update-database时创建.
升级到ASP.NET Core 2.0后无法创建迁移
我想在2个地方(这里和appsettings.json)没有ConnectionString,但只在.json,所以我试图更换
"Server=localhost;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true"
Run Code Online (Sandbox Code Playgroud)
同
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我得到空值.
更新1:
请注意,在Core 2中明确添加.json是不必要的,所以问题不在于文件.
https://andrewlock.net/exploring-program-and-startup-in-asp-net-core-2-preview1-2/
更新2:
此外,我已经使用Configuration从.json向Context发送ConnectionString:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); …Run Code Online (Sandbox Code Playgroud) 我想创建3个AppSettings配置文件:
在我的App.config中添加后:
<appSettings file="Database.config" />
<appSettings file="Messages.config" />
<appSettings file="Global.config" />
Run Code Online (Sandbox Code Playgroud)
但是当我尝试访问三个文件中的一个中的密钥时ConfigurationManager,我收到以下错误:
配置系统无法初始化.每个配置文件只能出现一次.
我不能有多个AppSettings配置文件?
我正在开发通用Windows平台应用程序,但Visual Studio中没有设置模板.
如何实现一个简单,强类型和可观察的类,将我的设置存储在LocalSettings或RoamingSettings中?
c# settings appsettings win-universal-app windows-10-universal
public class Bar
{
public static readonly string Foo = ConfigurationManager.AppSettings["Foo"];
}
Run Code Online (Sandbox Code Playgroud)
在.NET Framework 4.x的,我可以用ConfigurationManager.AppSettings ["Foo"]得到Foo的Webconfig,然后我可以轻松地获得的值Foo通过Bar.Foo
但是在.Net核心中,我必须注入options,并且无法获得Foo通过的价值Bar.Foo
有没有一种方法,可以直接通过Bar.Foo获取值来获得 Foo?
我的Web应用程序需要从appsettings.json文件中读取Document DB键.我创建了一个带有键名的类,并在以下位置读取Config部分ConfigureaServices():
public Startup(IHostingEnvironment env) {
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services.AddSession();
Helpers.GetConfigurationSettings(services, Configuration);
DIBuilder.AddDependency(services, Configuration);
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找在Test项目中读取Key值的方法.
关于恢复WinForm位置和大小的许多帖子.
例子:
但我还没有找到使用多个监视器执行此操作的代码.
也就是说,如果我使用监视器2上的窗口关闭我的.NET Winform应用程序,我希望它将窗口大小,位置和状态保存到应用程序设置,以便以后可以在我重新启动应用程序时恢复到监视器2.如果在上面的代码项目示例中它包含一些健全性检查,那将是很好的,因为如果保存的位置大部分是在屏幕外,它"修复"它.或者如果保存的位置在不再存在的显示器上(例如我的笔记本电脑现在没有我的第二台显示器),那么它会正确地将其移动到显示器1.
有什么想法吗?
我的环境:C#,.NET 3.5或更低版本,VS2008
我经常看到问题的答案如下:"我应该如何在我的.NET应用程序中存储设置?" 是通过手动将条目添加到app.config(或web.config)来编辑app.config文件,如下所示:
<configuration>
<appSettings>
**<add key="ConfigValueName" value="ABC"/>**
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
然后,访问它们像:
string configValue = Configuration.AppSettings["ConfigValueName"];
Run Code Online (Sandbox Code Playgroud)
我将上面概述的方法称为"app.config"方法.我很少看到人们建议在项目中添加"设置"文件.我已经在网络上和堆栈溢出中看到了这么多次...我开始怀疑我是否遗漏了某些东西......因为我不确定为什么你使用这种方法而不是使用"设置" "档案.我没有进入.NET直到VS2005所以我有一个理论是这是VS2003中的事情是如何完成的,人们从未切换过?
推荐app.config方法的人的示例:
从我的观点来看,"设置文件"方法有以下优点:
propertyGrid1.SelectedObject = Settings1.Default;就是你已经完成了.如果您不确定"设置"文件方法的含义,请参阅此帖子,这是有人建议使用设置文件而不是app.confg的少数示例之一.
编辑:请理解:本主题的目的是弄清楚为什么人们会使用上面概述的app.config方法而不是设置文件方法.我遇到了设置文件方法的限制,并且有时被迫推出自己的自定义解决方案.这是一个完全不同的讨论.
我创建了一个mvc站点,我将大量的json表单数据(Content-Type:application/x-www-form-urlencoded)发送回mvc控制器.当我这样做时,我收到500响应,声明:"InvalidDataException:超出表单值计数限制1024."
在以前版本的aspnet中,您可以将以下内容添加到web.config以增加限制:
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="5000" />
<add key="aspnet:MaxJsonDeserializerMembers" value="5000" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
当我将这些值放在web.config中时,我没有看到任何更改,所以我猜测Microsoft不再从web.config中读取这些值.但是,我无法弄清楚应该在哪里设置这些设置.
任何有关增加表单值计数的帮助都非常感谢!
需要说明的是,当我的帖子数据中的项目数小于1024时,此请求可以正常工作.
appsettings ×10
.net ×4
c# ×4
asp.net-core ×2
.net-2.0 ×1
app-config ×1
asp.net ×1
ios ×1
iphone ×1
objective-c ×1
properties ×1
settings ×1
unit-testing ×1
winforms ×1