我正在将ASP.NET 5 RC1项目迁移到ASP.NET Core,并且遇到了一个我还没有看到的有趣问题,或者找到了解决方案.
为了在Startup中使用配置设置,我以前通过以下方式重新配置了配置
// Works fine for DI both in ASP.NET 5 RC1 and ASP.NET Core
services.Configure<SomeConfigurationClass>(Configuration.GetSection("SomeConfigurationSection"));
// How I previous retrieved the configuration for use in startup.
// No longer available in ASP.NET Core
var someConfigurationToUseLater = Configuration.Get<SomeConfigurationClass>("SomeConfigurationSection");
Run Code Online (Sandbox Code Playgroud)
更新到ASP.NET Core 1.0后,似乎Configuration.Get <T>()不再可用.
我已经尝试更新代码以使用Configuration.GetValue <T>()但是这似乎不适用于对象,并且仅在提供值的路径时才起作用.这为我的大多数配置类留下了一个解决方法,就像这样
var someConfigurationName = "someConfiguration";
var someConfigurationClass = new SomeConfigurationClass()
{
Value1 = Configuration.GetValue<string>($"{someConfigurationName}:value1"),
Foo = Configuration.GetValue<string>($"{someConfigurationName}:foo"),
Bar = Configuration.GetValue<string>($"{someConfigurationName}:bar")
};
Run Code Online (Sandbox Code Playgroud)
但是,当配置类包含对象数组时,这是一个问题.在我的例子中是一个Client对象数组
public class ClientConfiguration
{
public Client[] Clients { get; set; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个基本的应用程序来学习ASP.NET 5.我觉得很困惑的一个领域是配置.在ASP.NET 5之前,我可以执行以下操作:
var settingValue = ConfigurationManager.AppSettings["SomeKey"];
Run Code Online (Sandbox Code Playgroud)
我会在我的代码中散布各种代码.现在,在vNext世界中,我有一个如下所示的config.json文件:
config.json
{
"AppSettings": {
"SomeKey":"SomeValue"
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Startup.cs中,我有以下内容: Startup.cs
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
Configuration = new Configuration()
.AddJsonFile("config.json");
}
Run Code Online (Sandbox Code Playgroud)
从那里,我完全难过.我在/src/Website/Code/Models/MyClass.cs中有MyClass.cs.
MyClass.cs
public class MyClass
{
public string DoSomething()
{
var result = string.Empty;
var keyValue = string.Empty; // TODO: What do I do here? How do I get the value of "AppSettings:SomeKey"?
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
如何获得"AppSettings:SomeKey"的值?
我很熟悉将appsettings.json部分加载到.NET Core startup.cs中的强类型对象中.例如:
public class CustomSection
{
public int A {get;set;}
public int B {get;set;}
}
//In Startup.cs
services.Configure<CustomSection>(Configuration.GetSection("CustomSection"));
//Inject an IOptions instance
public HomeController(IOptions<CustomSection> options)
{
var settings = options.Value;
}
Run Code Online (Sandbox Code Playgroud)
我有一个appsettings.json部分,其键/值对随着时间的推移会在数量和名称上有所不同.因此,对类中的属性名称进行硬编码是不切实际的,因为新的键/值对需要在类中进行代码更改.一些键/值对的小样本:
"MobileConfigInfo": {
"appointment-confirmed": "We've booked your appointment. See you soon!",
"appointments-book": "New Appointment",
"appointments-null": "We could not locate any upcoming appointments for you.",
"availability-null": "Sorry, there are no available times on this date. Please try another."
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将此数据加载到MobileConfigInfo Dictionary对象中,然后使用IOptions模式将MobileConfigInfo注入控制器?
我试图appsetting.json从类库中访问文件.到目前为止,我发现的解决方案是创建实现接口的配置类IConfiguration,从 Microsoft.Extensions.Configuration和JSON文件添加到类,并从相同的阅读.
var configuration = new Configuration();
configuration.AddJsonFile("appsetting.json");
var connectionString= configuration.Get("connectionString");
Run Code Online (Sandbox Code Playgroud)
这似乎是不好的选择,因为每次我们必须访问appsetting配置时我们必须添加json文件.我们不要有任何替代像ConfigurationManager在 ASP.NET.
web/api 应用程序中不再有 Startup.cs。
我们曾经能够注入IConfiguration到该类中Startup。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration
}
...
}
Run Code Online (Sandbox Code Playgroud)
现在这些添加服务和配置代码已移至Program.cs中,从那里访问配置的正确方法是什么?
我知道这仍处于预览阶段,但我只是想确保我没有做任何错误的事情,因为我过去做过类似的事情。我在属性中设置了环境变量:

我正在尝试设置我的测试:
[TestInitialize]
public void Initialize()
{
var test = Environment.GetEnvironmentVariables();
// test enumerates all the Env variables, don't see it there
var connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING");
if (string.IsNullOrWhiteSpace(connectionString)) // so this is obviously null
throw new ArgumentNullException("CONNECTION_STRING");
_ConnectionString = connectionString;
}
Run Code Online (Sandbox Code Playgroud)
正如您从我的评论中看到的,未找到/加载环境变量。
我缺少什么?谢谢。
我已经成功地在树莓派3上将asp.net核心mvc部署到Windows IoT核心。
我不确定UseUrls如以下代码片段所示通过调用指定侦听HTTP端口是否正确。
namespace winiotrasp
{
public class Program
{
// ... others ...
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://*:80")
.Build();
}
}
Run Code Online (Sandbox Code Playgroud)
是否通过UseUrls正确的方式指定了侦听HTTP端口?
请注意,如果我没有如上所示指定,则默认设置是http://localhost:5000使Web服务器无法从其他设备访问。
我需要从 .Net Core 项目中的数据库获取存储过程。通常我通过这样做来运行这个存储过程:
首选代码
readonly private SqlConnection _dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString);
public int Insert(Employee employee)
{
var result = 0;
using (var cmd = new SqlCommand("Sp_Insert", _dbConnection) { CommandType = CommandType.StoredProcedure })
{
try
{
cmd.Parameters.AddWithValue("@FirstName", employee.FirstName);
cmd.Parameters.AddWithValue("@LastName", employee.LastName);
cmd.Parameters.AddWithValue("@EmployeeCode", employee.EmployeeCode);
cmd.Parameters.AddWithValue("@Position", employee.Position);
cmd.Parameters.AddWithValue("@Office", employee.Office);
_dbConnection.Open();
result = cmd.ExecuteNonQuery();
}
catch
{
// ignore
}
finally
{
_dbConnection.Close();
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我的连接字符串位于 Web.config 中,但对于 .net Core,我的连接字符串位于 appsettings.json 中,如下所示:
.Net 实体框架代码
{
"ConnectionStrings": {
"Default": "server=DESKTOP-98TG6JE\\SERVER_2014;database=vega;user=sa;password=ComplexPassword!123;"
},
"Logging": …Run Code Online (Sandbox Code Playgroud) 我知道如何从 razor 组件访问 appsetting.json,但如何从 blazor 服务器端项目中的任何类访问?
从剃刀组件我只需注入:
@注入IConfiguration_config
并访问我需要的所有内容,例如:_config.GetConnectionString("default")
但如何在任何班级中做同样的事情呢?当我尝试做的时候
I配置_config;
它说当我想要访问数据时 _config 为空或者是一个未分配的局部变量取决于我写入变量的位置。