最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET?Core中的Configuration配置二

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

相關(guān)文章

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

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

1.內(nèi)存配置

MemoryConfigurationProvider使用內(nèi)存中集合作為配置鍵值對(duì)。若要激活內(nèi)存中集合配置,請(qǐng)?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>();
}

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

1.1GetValue

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

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

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

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


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

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

可以使用選項(xiàng)模式將文件配置綁定到相關(guān)實(shí)體類。配置值作為字符串返回,但調(diào)用Bind 可以綁定POCO對(duì)象。Bind在Microsoft.Extensions.Configuration.Binder包中,后者在 Microsoft.AspNetCore.App元包中?,F(xiàn)在我們?cè)贑oreWeb/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è)對(duì)應(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鍵。通過(guò)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.綁定至對(duì)象圖

通過(guò)第2小節(jié)我們學(xué)習(xí)到如何綁定配置文件內(nèi)容映射到實(shí)例化實(shí)體類屬性去,同樣,配置文件內(nèi)容也可以綁定到對(duì)象圖去?,F(xiàn)在我們?cè)贑oreWeb/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è)對(duì)應(yīng)配置內(nèi)容的實(shí)體模型(/Models/TvShow.cs),其對(duì)象圖包含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對(duì)象圖。將綁定實(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對(duì)象圖:

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ù)組綁定到對(duì)象類去。公開(kāi)數(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(wú)法綁定null值,所以該數(shù)組跳過(guò)了索引#3的值。在示例應(yīng)用程序中,POCO類可用于保存綁定的配置數(shù)據(jù):

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

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

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

還可以使用ConfigurationBinder.Get<T>語(yǔ)法,從而產(chǎn)生更精簡(jiǎ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頁(yè)或MVC視圖中訪問(wèn)配置

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

@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>

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

相關(guān)文章

  • C#Web應(yīng)用程序入門(mén)經(jīng)典學(xué)習(xí)筆記之一

    C#Web應(yīng)用程序入門(mén)經(jīng)典學(xué)習(xí)筆記之一

    C#Web應(yīng)用程序入門(mén)經(jīng)典學(xué)習(xí)筆記之一...
    2006-08-08
  • .NET1.0版本中的異步編程模型(APM)

    .NET1.0版本中的異步編程模型(APM)

    這篇文章介紹了.NET1.0版本中的異步編程模型(APM),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#聲明方法實(shí)例說(shuō)明

    C#聲明方法實(shí)例說(shuō)明

    方法(Method)是一個(gè)已命名的語(yǔ)句集。如果以前使用過(guò)其他編程語(yǔ)言,如C或Visual Basic等,就可以將方法視為函數(shù)或者子程序相似的東西。每個(gè)方法都有一個(gè)名稱和一個(gè)主體。方法名應(yīng)該是一個(gè)有意義的標(biāo)識(shí)符,它應(yīng)描述出方法的用途(如CalculateIncomeTax)。方法主體包含了調(diào)用方法時(shí)實(shí)際執(zhí)行的語(yǔ)句。你可以為大多數(shù)方法提供一些數(shù)據(jù)來(lái)進(jìn)行處理,并讓它返回一些信息(通常是處理結(jié)果)。方法是一種基本的、功能強(qiáng)大的編程機(jī)制。
    2008-04-04
  • Blazor路由與頁(yè)面導(dǎo)航開(kāi)發(fā)介紹

    Blazor路由與頁(yè)面導(dǎo)航開(kāi)發(fā)介紹

    這篇文章介紹了Blazor路由與頁(yè)面導(dǎo)航開(kāi)發(fā),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • 詳解ABP框架中的數(shù)據(jù)過(guò)濾器與數(shù)據(jù)傳輸對(duì)象的使用

    詳解ABP框架中的數(shù)據(jù)過(guò)濾器與數(shù)據(jù)傳輸對(duì)象的使用

    ABP框架是一個(gè)基于ASP.NET的Web開(kāi)發(fā)框架,這里我們來(lái)詳解ABP框架中的數(shù)據(jù)過(guò)濾器與數(shù)據(jù)傳輸對(duì)象的使用,需要的朋友可以參考下
    2016-06-06
  • .Net設(shè)計(jì)模式之單例模式(Singleton)

    .Net設(shè)計(jì)模式之單例模式(Singleton)

    這篇文章介紹了.Net設(shè)計(jì)模式之單例模式(Singleton),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • ASP.NET MVC中_ViewStart.cshtml作用介紹

    ASP.NET MVC中_ViewStart.cshtml作用介紹

    這篇文章介紹了ASP.NET MVC中_ViewStart.cshtml的作用,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • asp.net UpdaeProgress的簡(jiǎn)單用法

    asp.net UpdaeProgress的簡(jiǎn)單用法

    這個(gè)控件相比其他控件,屬性少 使用簡(jiǎn)單,就先把這個(gè)控件的一般使用方法簡(jiǎn)單紀(jì)錄下
    2008-10-10
  • ASP.NET?Core中的靜態(tài)文件

    ASP.NET?Core中的靜態(tài)文件

    這篇文章介紹了ASP.NET?Core中的靜態(tài)文件,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • ASP.NET 5已終結(jié),迎來(lái)ASP.NET Core 1.0和.NET Core 1.0

    ASP.NET 5已終結(jié),迎來(lái)ASP.NET Core 1.0和.NET Core 1.0

    命名是非常困難的事情,微軟這次為了和ASP.NET4.6做區(qū)分,采用了全新的命名方式ASP.NET Core 1.0,它是一個(gè)全新的框架。
    2016-03-03

最新評(píng)論

桃江县| 油尖旺区| 来安县| 资阳市| 兴隆县| 浦县| 美姑县| 保亭| 宁蒗| 延吉市| 南溪县| 黄龙县| 外汇| 景东| 饶阳县| 台中市| 冕宁县| 扎兰屯市| 朝阳县| 兰州市| 龙山县| 牡丹江市| 乌苏市| 盐边县| 朝阳市| 连江县| 睢宁县| 铜梁县| 永新县| 中卫市| 固安县| 方山县| 浙江省| 布拖县| 东兰县| 宜兰市| 巩留县| 同仁县| 襄垣县| 东台市| 禄丰县|