[英]ASP.NET 5 (vNext) - Getting a Configuration Setting
我正在編寫一個基本的應用程序來學習ASP.NET 5.我覺得很困惑的一個領域是配置。 在ASP.NET 5之前,我可以執行以下操作:
var settingValue = ConfigurationManager.AppSettings["SomeKey"];
我會在我的代碼中散布各種代碼。 現在,在vNext世界中,我有一個如下所示的config.json文件:
config.json
{
"AppSettings": {
"SomeKey":"SomeValue"
}
}
然后在Startup.cs中,我有以下內容: Startup.cs
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
Configuration = new Configuration()
.AddJsonFile("config.json");
}
從那里,我完全難過。 我在/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;
}
}
如何獲得“AppSettings:SomeKey”的值?
ASP.NET 5大量使用依賴注入,所以如果你也使用依賴注入,那么這很簡單。 如果您檢查示例MVC6項目,您可以看到它是如何工作的:
首先,在屬性中定義了一個類AppSettings,它是您的類支持的選項的強類型版本。 在示例項目中,它只包含SiteTitle。
public class AppSettings
{
public string SiteTitle { get; set; }
}
然后,通過ConfigureServices中的依賴注入初始化此類。 這里的Configuration
是您在Startup類的構造函數中創建的Configuration
。
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
// ...
}
然后,假設您的類由依賴注入容器實例化,您可以簡單地請求IOptions,然后您將獲得一個。 例如,在控制器中,您可以具有以下內容:
public class HomeController
{
private string _title;
public HomeController(IOptions<AppSettings> settings)
{
_title = settings.Options.SiteTitle;
}
}
我使用ASP.NET 5依賴注入,就像這樣。
config.json:
{
"random": "Hello World!"
}
startup.cs:
public class Startup
{
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json");
Configuration = builder.Build();
}
public IConfiguration Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IConfiguration>(sp => { return Configuration; });
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes =>
{
routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
控制器:
public class HomeController : Controller
{
IConfiguration config;
public HomeController(IConfiguration config)
{
this.config = config;
}
public IActionResult Index()
{
var template = "<marquee>{0}</marquee>";
var content = string.Format(template, config.Get("random"));
return Content(content, "text/html");
}
}
我強烈建議使用OptionsModel
而不是直接讀取配置。 它允許強類型模型綁定到配置。
下面是一個示例: GitHub.com/aspnet/Options/test/Microsoft.Extensions.Options.Test/OptionsTest.cs
對於您的特定情況,創建一個模型:
class AppSettings {
public string SomeSetting {get;set;}
}
然后將其綁定到您的配置:
var config = // The configuration object
var options = ConfigurationBinder.Bind<AppSettings>(config);
Console.WriteLine(options.SomeSetting);
這樣您就不必擔心設置的來源,存儲方式或結構。 您只需預定義您的選項模型並發生魔術。
用這個:
var value = Configuration.Get("AppSettings:SomeKey");
基於這篇博文 。 冒號類似於點表示法,用於在層次結構中向下導航。
如果你需要其他類中的值,你應該將其注入.ASP.NET已內置依賴注入,但如果你只需要一個MyClass實例,你可以新建它而不是設置一個DI容器。
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
Configuration = new Configuration()
.AddJsonFile("config.json");
//generally here you'd set up your DI container. But for now we'll just new it up
MyClass c = new MyClass(Configuration.Get("AppSettings:SomeKey"));
}
public class MyClass
{
private readonly string Setting; //if you need to pass multiple objects, use a custom POCO (and interface) instead of a string.
public MyClass(string setting) //This is called constructor injection
{
Setting = setting;
}
public string DoSomething()
{
var result = string.Empty;
//Use setting here
return result;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.