2015-06-28 64 views
1

在ASP.NET 4中組織設置,我在設置密鑰前添加一個小字,指示使用此配置的位置(例如key =「dms:url」,「sms:fromNumber」....等)。如何處理ASP.NET 5 AppSettings中的屬性層次結構?

在ASP.NET 5中,AppSettings配置映射到強類型類。 什麼是我需要爲「dms:url」構建的屬性?如何將特殊字符&映射爲ASP.NET 5中的C#屬性?

回答

2

您可以在config.json

{ 
    "AppSettings": { 
    "SiteTitle": "PresentationDemo.Web", 
    "Dms": { 
     "Url": "http://google.com", 
     "MaxRetries": "5" 
    }, 
    "Sms": { 
     "FromNumber": "5551234567", 
     "APIKey": "fhjkhededeudoiewueoi" 
    } 
    }, 
    "Data": { 
    "DefaultConnection": { 
     "ConnectionString": "MyConnectionStringHere. Included to show you can use the same config file to process both strongly typed and directly referenced values" 
    } 
    } 
} 

我們定義的AppSettings爲POCO類層次結構中組織您的配置文件。

public class AppSettings 
{ 
    public AppSettings() 
    { 
     Dms = new Dms(); // need to instantiate (Configuration only sets properties not create the object) 
     Sms = new Sms(); // same 
    } 

    public string SiteTitle { get; set; } 
    public Dms Dms { get; set; } 
    public Sms Sms { get; set; } 
} 

public class Dms 
{ 
    public string Url { get; set; } 
    public int MaxRetries { get; set; } 
} 

public class Sms 
{ 
    public string FromNumber { get; set; } 
    public string ApiKey { get; set; } 
} 

我們然後加載配置成使用GetSubKey的AppSettings的IConfigurationSourceRoot,然後設定值的實例。最佳做法是在ConfigureServices中執行此操作並將其添加到DI容器中。

public class Startup 
{ 
    public Startup(IHostingEnvironment env) 
    { 
     // Setup configuration sources. 
     var configuration = new Configuration() 
      .AddJsonFile("config.json") 
      .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true); 
    } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     // Add Application settings to the services container. 
     services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings")); 

     //Notice we can also reference elements directly from Configuration using : notation 
     services.AddEntityFramework() 
      .AddSqlServer() 
      .AddDbContext<ApplicationDbContext>(options => 
       options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); 
    } 
} 

我們現在可以通過構造函數在控制器中提供訪問權限。我設定的設定值明確的構造函數,但如果你想保留一切平整,使用僞層次就像你一直在做,你可以使用整個IOptions

public class HomeController : Controller 
{ 
    private string _title; 
    private string _fromNumber; 
    private int _maxRetries; 

    public HomeController(IOptions<AppSettings> settings) 
    { 
     _title = settings.Options.SiteTitle; 
     _fromNumber = settings.Options.Sms.FromNumber; 
     _maxRetries = settings.Options.Dms.MaxRetries; 
    } 

,你可以,但「:」是不是變量名的有效符號。您需要使用有效的符號,如「_」或「 - 」。

+0

Hi @ gerald-davis,看起來我需要在Startup.cs中重新配置一些東西 你能指導我嗎? 我的意思是: var appSettingsConfig = services.Configure (Configuration.GetSubKey(「AppSettings」)); 將是不夠的!對?! 我得到了對象引用異常,因爲AppSettings中的Dms屬性爲null。 我創建了Dms類,然後在AppSettings類中創建了一個名爲Dms的屬性。 – bunjeeb

+1

使用上面的配置文件沒有AppSetting的Dms屬性。使用上面的配置文件將以AppSettings.Options.Dms.FromNumber爲例。請注意屬性鏈中的「選項」。我通過手機迴應。當我到達工作站時,我將更新startup.cs中的初始化代碼(除非其他人)。 –

+0

謝謝你的回覆,等待你的答案..因爲我找不到有用的博客文章解釋..也許如果你知道一些資源,請發送給我。 – bunjeeb