设置了JSON文件在我Startup.cs文件我有以下几点:无法读取Startup.cs
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var fooValue = Configuration.Get("foo");
// Add framework services.
services.AddMvc();
}
在我appsettings.json文件我有{ "foo": "bar" }
在我的启动类后来我:
public void ConfigureServices(IServiceCollection services)
{
var fooValue = Configuration.Get("foo");
// Add framework services.
services.AddMvc();
}
我尝试观看到fooValue变化值在我的当地人通过它设置断点(与ASP.NET 5扩展VS2015),调试器,但我得到以下错误,当我跨过它:
“System.InvalidOperationException”类型发生在微软的一个例外.Extensions.Configuration.Binder.dll但未在用户代码中处理
其他信息:无法创建类型为'System.String'的实例,因为它缺少一个无公共的无参数构造函数。
我正在关注的MVA Configuring Data at 18m 53s
我试图创建一个config.json文件作为例子,但模板似乎已经改变了,它不反正帮助。
我应该被允许从这样的.json文件中获取配置信息吗?
下面是一个类似的问题与工作示例:http://stackoverflow.com/a/32044957/390421(其中包括加载json数据并通过config.get()调用访问它。) – MartijnK
此外,你可以找到有关如何在此处访问嵌套json属性的更多示例:http://whereslou.com/2014/05/23/asp-net-vnext-moving-parts-iconfiguration/ – MartijnK