我正在使用VS Ultimate 2015 Preview开发一个ASP.NET 5 WebAPI项目.我正在尝试以这种方式配置应用程序(行号只是指南):
1 using Microsoft.Framework.ConfigurationModel;
2
3 public IConfiguration Configuration { get; private set; }
4
5 public Startup()
6 {
7 Configuration = new Configuration()
8 .AddJsonFile("config.json")
9 .AddEnvironmentVariables();
10 }
Run Code Online (Sandbox Code Playgroud)
第8行给出了一个错误:'Configuration'不包含'AddJsonFile'的定义......
怎么了?.
c# configuration configuration-files config.json asp.net-core
我正在关注ASP.NET 5的一些示例,我偶然发现了如何正确读取"嵌套"配置值(如果这是正确的术语).
以下是相关部分config.json
:
{
"ApplicationName" : "OwNextApp",
"AppSettings": {
"SiteTitle": "OwNext"
},
}
Run Code Online (Sandbox Code Playgroud)
相关部分HomeController.cs
:
public IActionResult About()
{
var appNestedNameFailed = _config.Get("AppSettings.SiteTitle");
var appNestedNameSuccess = _config.Get("AppSettings:SiteTitle");
var appName = _config.Get("ApplicationName");
ViewBag.Message = string.Format(@"Your
APP NAME: {0};
APP NESTED NAME FAILED: {1};
APP NESTED NAME SUCCESS: {2}",
appName, appNestedNameFailed, appNestedNameSuccess);
return View();
}
Run Code Online (Sandbox Code Playgroud)
对于值appNestedNameFailed
是空的(研究之前我最初的尝试).并且appNestedNameSuccess
有价值; 在我进行研究并在配置测试中找到(相关代码显示):
// Assert
Assert.Equal("IniValue1", config.Get("IniKey1"));
Assert.Equal("IniValue2", config.Get("IniKey2:IniKey3"));
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会这样吗?为什么使用:
结束有意义.
?从我与JSON数据的交互,通常. …
我有一个基于MVC 6(beta 3)的基本Web API控制器作为新ASP.NET项目的一部分.我遇到的问题是它不接受动词PUT或PATCH,并且每当我尝试使用这些方法访问URL时都会返回错误404.
这是我作为基本测试得到的:
namespace Test.Controllers
{
[Route("api/test")]
public class TestController : Controller
{
[HttpGet]
public string TestGet()
{
return "Hello from GET!";
}
[HttpPost]
public string TestPost()
{
return "Hello from POST!";
}
[HttpDelete]
public string TestDelete()
{
return "Hello from DELETE!";
}
[HttpPut]
public string TestPut()
{
return "Hello from PUT!";
}
[HttpPatch]
public string TestPatch()
{
return "Hello from PATCH!";
}
}
}
Run Code Online (Sandbox Code Playgroud)
访问http://localhost/api/test
'Postman'来检查每个动词(GET,POST,DELETE,PUT和PATCH)的URL依次适用于GET,POST和DELETE,但是给出了带有PUT和PATCH的404.
编辑:
我记得有一种方法可以为MVC5及更低版本启用这些动词,包括禁用WebDAV并为这两个动词添加处理程序web.config
,但由于没有像web.config
ASP.NET 5 这样的东西,我完全失去了如何解决这个问题.我假设它可能已经解决了config.json
但是我所有尝试搜索这个都没有任何帮助! …
假设我有一个config.json
这样的:
{
"CustomSection": {
"A": 1,
"B": 2
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用IConfiguration
对象来获取特定设置,即 ,configuration.Get("CustomSection:A")
但是我可以获取整个层次结构(任何类型 - 即使是原始字符串也可以)?当我尝试时configuration.Get("CustomSection")
,我得到了null
结果,所以我认为默认情况下不支持。
我的用例是一次获取整个配置字典,而不必获取每个单独的设置 - 在编译时可能不知道某些属性。
我正在开发n层应用程序,我有数据访问层.这与任何其他应用程序无关.我已经为.NET Core 1.1创建了类库,我可以看到依赖项文件夹但不能查看任何config/JSON文件.我想知道,我可以在类库项目中添加AppSetting.JSON文件吗?接下来我该如何添加连接字符串.我知道在DbContext类 - > SQLServerConnection中覆盖ConfigureBuilder但我想保留在单独的配置文件中并引用这些连接字符串.
另请注意,此类库不会直接链接到ASP.NET应用程序
在谷歌搜索时,我发现以下链接https://www.stevejgordon.co.uk/project-json-replaced-by-csproj但我的问题仍然存在,我在哪里以及如何为类库项目添加连接字符串?
c# class-library entity-framework-core .net-core config.json
我遇到过使用这两种方法进行配置管理的人.
每种方法的优缺点是什么?
如果我在配置对象中存储了很多变量,那么在执行节点应用程序之前,我是否必须在upstart脚本中逐个设置它们?