亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET?Core中的Configuration配置二

 更新時(shí)間:2022年04月07日 10:21:32   作者:暗斷腸  
這篇文章介紹了ASP.NET?Core中的Configuration配置,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

相關(guān)文章

ASP.NET Core2.2 中的Configuration配置一

ASP.NET Core2.2 中的Configuration配置二

1.內(nèi)存配置

MemoryConfigurationProvider使用內(nèi)存中集合作為配置鍵值對。若要激活內(nèi)存中集合配置,請?jiān)贑onfigurationBuilder的實(shí)例上調(diào)用AddInMemoryCollection擴(kuò)展方法??梢允褂肐Enumerable<KeyValuePair<String,String>> 初始化配置提供程序。構(gòu)建主機(jī)時(shí)調(diào)用ConfigureAppConfiguration以指定應(yīng)用程序的配置:

public class Program
{
    public static readonly Dictionary<string, string> _dict =
        new Dictionary<string, string>
        {
            {"MemoryCollectionKey1", "value1"},
            {"MemoryCollectionKey2", "value2"}
        };
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddInMemoryCollection(_dict);
            })
            .UseStartup<Startup>();
}

而通過啟動(dòng)應(yīng)用程序時(shí)會(huì)看到如下配置信息:

1.1GetValue

ConfigurationBinder.GetValue<T>從具有指定鍵的配置中提取一個(gè)值,并可以將其轉(zhuǎn)換為指定類型。如果未找到該鍵,則獲取配置默認(rèn)值。如上述示例中,配置兩個(gè)value1、value2值,現(xiàn)在我們在鍵MemoryCollectionKey1配置中提取對應(yīng)字符串值,如果找不到配置鍵MemoryCollectionKey1,則默認(rèn)使用value3配置值,示例代碼如下:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    var config = Configuration.GetValue<string>("MemoryCollectionKey1", "value3");
}

而通過啟動(dòng)應(yīng)用程序時(shí)會(huì)看到如下配置信息:

ConfigurationBinder.GetValue找到定義string類型MemoryCollectionKey1鍵值并輸出。如果我們把獲取鍵名稱更改為MemoryCollectionKey3,再來看看獲取鍵值輸出結(jié)果:


我們會(huì)看到當(dāng)ConfigurationBinder.GetValue找不到定義string類型MemoryCollectionKey3鍵時(shí),則輸出默認(rèn)值。

2.綁定到實(shí)體類

可以使用選項(xiàng)模式將文件配置綁定到相關(guān)實(shí)體類。配置值作為字符串返回,但調(diào)用Bind 可以綁定POCO對象。Bind在Microsoft.Extensions.Configuration.Binder包中,后者在 Microsoft.AspNetCore.App元包中?,F(xiàn)在我們在CoreWeb/Models目錄下新增一個(gè)叫starship.json文件,配置內(nèi)容如下:

{
  "starship": {
    "name": "USS Enterprise",
    "registry": "NCC-1701",
    "class": "Constitution",
    "length": 304.8,
    "commissioned": false
  },
  "trademark": "Paramount Pictures Corp. http://www.paramount.com"
}

然后再新增一個(gè)對應(yīng)配置內(nèi)容的實(shí)體模型(/Models/Starship.cs):

public class Starship
{
    public string Name { get; set; }
    public string Registry { get; set; }
    public string Class { get; set; }
    public decimal Length { get; set; }
    public bool Commissioned { get; set; }
}

構(gòu)建主機(jī)時(shí)調(diào)用ConfigureAppConfiguration以指定應(yīng)用程序的配置:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.SetBasePath(Directory.GetCurrentDirectory());
            config.AddJsonFile(
                "starship.json", optional: true, reloadOnChange: true);
        })
        .UseStartup<Startup>();

示例應(yīng)用程序調(diào)用GetSection方法獲取json文件中starship鍵。通過Bind方法把starship鍵屬性值綁定到Starship類的實(shí)例中:

var starship = new Starship();
Configuration.GetSection("starship").Bind(starship);
var _starship = starship;

當(dāng)應(yīng)用程序啟動(dòng)時(shí)會(huì)提供JSON文件配置內(nèi)容:

3.綁定至對象圖

通過第2小節(jié)我們學(xué)習(xí)到如何綁定配置文件內(nèi)容映射到實(shí)例化實(shí)體類屬性去,同樣,配置文件內(nèi)容也可以綁定到對象圖去?,F(xiàn)在我們在CoreWeb/Models目錄下新增一個(gè)叫tvshow.xml文件,配置內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <tvshow>
    <metadata>
      <series>Dr. Who</series>
      <title>The Sun Makers</title>
      <airdate>11/26/1977</airdate>
      <episodes>4</episodes>
    </metadata>
    <actors>
      <names>Tom Baker, Louise Jameson, John Leeson</names>
    </actors>
    <legal>(c)1977 BBC https://www.bbc.co.uk/programmes/b006q2x0</legal>
  </tvshow>
</configuration>

然后再新增一個(gè)對應(yīng)配置內(nèi)容的實(shí)體模型(/Models/TvShow.cs),其對象圖包含Metadata和 Actors類:

public class TvShow
{
    public Metadata Metadata { get; set; }
    public Actors Actors { get; set; }
    public string Legal { get; set; }
}
public class Metadata
{
    public string Series { get; set; }
    public string Title { get; set; }
    public DateTime AirDate { get; set; }
    public int Episodes { get; set; }
}
public class Actors
{
    public string Names { get; set; }
}

構(gòu)建主機(jī)時(shí)調(diào)用ConfigureAppConfiguration以指定應(yīng)用程序的配置:
config.AddXmlFile("tvshow.xml", optional: true, reloadOnChange: true);
使用Bind方法將配置內(nèi)容綁定到整個(gè)TvShow對象圖。將綁定實(shí)例分配給用于呈現(xiàn)的屬性:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    var tvShow = new TvShow();
    Configuration.GetSection("tvshow").Bind(tvShow);
    var _tvShow = tvShow;
}

當(dāng)應(yīng)用程序啟動(dòng)時(shí)會(huì)提供XML文件配置內(nèi)容:

還有一種Bind方法可以將配置內(nèi)容綁定到整個(gè)TvShow對象圖:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    var _tvShow = Configuration.GetSection("tvshow").Get<TvShow>();
}

當(dāng)應(yīng)用程序啟動(dòng)時(shí)會(huì)提供XML文件配置內(nèi)容:

4.將數(shù)組綁定至類

Bind方法也支持把配置內(nèi)容鍵中的數(shù)組綁定到對象類去。公開數(shù)字鍵段(:0:、:1:、… :{n}:)的任何數(shù)組格式都能夠與POCO類數(shù)組進(jìn)行綁定。使用內(nèi)存配置提供應(yīng)用程序在示例中加載這些鍵和值:

public class Program
{
    public static Dictionary<string, string> arrayDict =
            new Dictionary<string, string>
            {
                {"array:entries:0", "value0"},
                {"array:entries:1", "value1"},
                {"array:entries:2", "value2"},
                {"array:entries:4", "value4"},
                {"array:entries:5", "value5"}
            };
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory());
                config.AddInMemoryCollection(arrayDict);
            })
            .UseStartup<Startup>();
}

因?yàn)榕渲媒壎ǔ绦驘o法綁定null值,所以該數(shù)組跳過了索引#3的值。在示例應(yīng)用程序中,POCO類可用于保存綁定的配置數(shù)據(jù):

public class ArrayExample
{
    public string[] Entries { get; set; }
}

將配置數(shù)據(jù)綁定至對象:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    var arrayExample = new ArrayExample();
    Configuration.GetSection("array").Bind(arrayExample);
    var _arrayExample = arrayExample;
}

還可以使用ConfigurationBinder.Get<T>語法,從而產(chǎn)生更精簡的代碼:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    var _arrayExample = _config.GetSection("array").Get<ArrayExample>();
}

當(dāng)應(yīng)用程序啟動(dòng)時(shí)會(huì)提供內(nèi)存配置內(nèi)容:

5.在Razor Pages頁或MVC視圖中訪問配置

若要訪問RazorPages頁或MVC視圖中的配置設(shè)置,請為Microsoft.Extensions.Configuration命名空間添加using指令(C#參考:using指令)并將IConfiguration注入頁面或視圖。
在Razor頁面頁中:

@page
@model IndexModel
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Index Page</title>
</head>
<body>
    <h1>Access configuration in a Razor Pages page</h1>
    <p>Configuration value for 'key': @Configuration["key"]</p>
</body>
</html>

在MVC視圖中:

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Index View</title>
</head>
<body>
    <h1>Access configuration in an MVC view</h1>
    <p>Configuration value for 'key': @Configuration["key"]</p>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論