Tag: appsettings

如何在Asp.Net MVC 6中检索AppSettings配置?

假设我使用新的DepencyInjection框架在新的ASP.Net/vNext中配置我的类和依赖项。 我该如何使用,如何获取预定义的配置设置? public void ConfigureServices(IServiceCollection services) { // Add Application settings to the services container. services.Configure(Configuration.GetSubKey(“AppSettings”)); // Add EF services to the services container. services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration[“Data:DefaultConnection:ConnectionString”])); // Add Identity services to the services container. services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); // Configure the options for the authentication middleware. // You can add options for Google, […]

访问控制器类中的appsettings.json值

无法弄清楚如何读取startup.cs之外的appsettings.json值。 我想做的是,例如,在_Layout.cshtml中,从配置中添加站点名称: 例如: ViewData[“SiteName”] = Configuration.GetValue(“SiteSettings:SiteName”); 甚至更好: public class GlobalVars { public static string SiteName => Configuration.GetValue(“SiteSettings:SiteName”); } 到目前为止,这是我的代码: [appsettings.json] “SiteSettings”: { “SiteName”: “MySiteName” } [startup.cs] public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true) .AddJsonFile($”appsettings.{env.EnvironmentName}.json”, optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); var siteName = Configuration.GetValue(“SiteSettings:SiteName”); } public IConfigurationRoot […]

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

我以前曾经多次使用过这两个配置文件,但我从来没有花时间完全理解它们是如何工作的。 正如大多数人所做的那样,我理解了如何调用WebConfigurationManager.AppSettings[“key”]来获取配置值的基础知识。 以下是我提出的一些问题: 在类库中引用配置值时会发生什么,并且库是更大解决方案的一部分? 是否需要将app.config复制到输出目录才能找到变量? (我假设是的) 你能直接在另一个类库中使用app.config的配置值吗? 假设问题3为“是”,如果来自不同库的多个app.config文件包含具有相同键的配置值,会发生什么? 引用web.config但在类库中会发生什么? 当您引用app.config但在网站或Web应用程序项目中时会发生什么?

从.NET Core 2中的类读取appsettings.json

我需要从业务类中的appsettings.json文件(section: placeto )中读取属性列表,但我无法访问它们。 我需要公开这些属性。 我在Program类中添加了该文件: 这是我的appsettings.json : { “Logging”: { “IncludeScopes”: false, “LogLevel”: { “Default”: “Warning” } }, “placeto”: { “login”: “fsdfsdfsfddfdfdfdf”, “trankey”: “sdfsdfsdfsdfsdf” } }

ConfigurationSettings.AppSettings已过时,警告

var values = new NameValueCollection { { “key”, ConfigurationSettings.AppSettings[“API-Key”].ToString() }, { “image”, Convert.ToBase64String(File.ReadAllBytes(photo.ToString())) } }; 使用app.config文件的新方法是什么?

获取另一个Exe的App.Config

我有一个带有App.Config文件的exe。 现在我想在exe周围创建一个包装器dll,以便消耗一些function。 问题是如何从包装器DLL访问exe中的app.config属性? 也许我应该在我的问题中多一点,我有以下app.config内容与exe: 问题是如何从包装器DLL中取出“myValue”? 谢谢你的解决方案。 实际上我最初的概念是避免使用XML文件读取方法或LINQ或其他任何方法。 我首选的解决方案是使用配置管理器库等 。 我将非常感谢使用通常与访问app.config属性相关联的类的任何帮助。

ASP.NET Core appsettings.json在代码中更新

我目前正在使用asp.net core v1.1开发项目,在我的appsettings.json中我有: “AppSettings”: { “AzureConnectionKey”: “***”, “AzureContainerName”: “**”, “NumberOfTicks”: 621355968000000000, “NumberOfMiliseconds”: 10000, “SelectedPvInstalationIds”: [ 13, 137, 126, 121, 68, 29 ], “MaxPvPower”: 160, “MaxWindPower”: 5745.35 }, 我也有用来存储它们的类: public class AppSettings { public string AzureConnectionKey { get; set; } public string AzureContainerName { get; set; } public long NumberOfTicks { get; set; } public long […]

AppSettings.json用于ASP.NET核心中的集成测试

我正在遵循本指南 。 我在API项目中有一个使用appsettings.json配置文件的appsettings.json 。 public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .ReadFrom.Configuration(Configuration) .CreateLogger(); } 我正在看的特定部分是env.ContentRootPath 。 我做了一些挖掘,看起来我的appsettings.json实际上并没有复制到bin文件夹,但是这很好,因为ContentRootPath返回了MySolution\src\MyProject.Api\ ,这是appsettings.json文件所在的位置。 所以在我的集成测试项目中,我有这个测试: public class TestShould { private readonly TestServer _server; private readonly HttpClient _client; public TestShould() { […]

如何将ConfigurationManager.AppSettings与自定义部分一起使用?

我需要使用App.config文件获取“ http://example.com ”。 但目前我正在使用: string peopleXMLPath = ConfigurationManager.AppSettings[“server”]; 我无法获得价值。 你能指出我做错了什么吗?

如何在安装时设置应用程序设置(通过安装程序类)

我有一个安装程序类的Visual Studio安装项目。 在安装程序类中,我设置如下设置: MessageBox.Show(Properties.Settings.Default.MySetting); Properties.Settings.Default.MySetting = “Foo”; Properties.Settings.Default.Save(); MessageBox.Show(Properties.Settings.Default.MySetting); 问题是,即使我知道这个代码正在执行(我正在做其他的东西),设置永远不会设置! 消息框确实表明正在设置该值,但是当我转到.config文件时,该值仍为空白! 任何人有任何想法和/或可能的解决方法?