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

詳解ASP.NET Core 中的框架級依賴注入

 更新時間:2017年10月24日 14:32:54   作者:Oopsguy  
本篇文章主要介紹了詳解ASP.NET Core 中的框架級依賴注入,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1、ASP.NET Core 中的依賴注入

此示例展示了框架級依賴注入如何在 ASP.NET Core 中工作。 其簡單但功能強大,足以完成大部分的依賴注入工作。框架級依賴注入支持以下 scope:

  1. Singleton — 總是返回相同的實例
  2. Transient — 每次都返回新的實例
  3. Scoped — 在當(dāng)前(request)范圍內(nèi)返回相同的實例

假設(shè)我們有兩個要通過依賴注入來進(jìn)行工作的工件:

  1. PageContext — 自定義請求上下文
  2. Settings — 全局應(yīng)用程序設(shè)置

這兩個都是非常簡單的類。PageContext 類為布局頁面提供當(dāng)前頁面標(biāo)題的標(biāo)題標(biāo)簽。

public class Settings 
{
 public string SiteName;
 public string ConnectionString;
}
public class PageContext
{
  private readonly Settings _settings;
  public PageContext(Settings settings)
  {
    _settings = settings;
  }
  public string PageTitle;
  public string FullTitle
  {
    get
    {
      var title = (PageTitle ?? "").Trim(); 
      if(!string.IsNullOrWhiteSpace(title) &&
        !string.IsNullOrWhiteSpace(_settings.SiteName))
      {
        title += " | ";
      }
      title += _settings.SiteName.Trim();
      return title;
    }
  }
}

2、注冊依賴

在 UI 構(gòu)建塊中使用這些類之前,需要在應(yīng)用程序啟動時注冊這些類。該工作可以在 Startup 類的 ConfigureServices() 方法中完成。

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  var settings = new Settings();
  settings.SiteName = Configuration["SiteName"];
  services.AddSingleton(settings);
  services.AddScoped<PageContext>();
}

現(xiàn)在可以將這些類注入到支持依賴注入的控制器和其他 UI 組件中。

3、向控制器注入實例

我們通過 Home 控制器中的 PageContext 類分配頁面標(biāo)題。

public class HomeController : Controller
{
  private readonly PageContext _pageContext;
  public HomeController(PageContext pageContext)
  {
    _pageContext = pageContext;
  }
  public IActionResult Index()
  {
    _pageContext.PageTitle = "";
    return View();
  }
  public IActionResult About()
  {
    _pageContext.PageTitle = "About";
    return View();
  }
  public IActionResult Error()
  {
    _pageContext.PageTitle = "Error";
 
    return View();
  }
}

這種分配頁面標(biāo)題的方式不錯,因為我們不必使用 ViewData,這樣更容易受支持多語言應(yīng)用程序支持。

4、向視圖注入實例

現(xiàn)在控制器的 action 中分配了頁面標(biāo)題,是時候在布局頁面中使用標(biāo)題了。 我在頁面的內(nèi)容區(qū)域添加了標(biāo)題,所以在 tech.io 環(huán)境中也很容易看到。為了能在布局頁面中使用到 PageContext,我使用了視圖注入(下面代碼片段中的第一行)。

@inject PageContext pageContext
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>@pageContext.FullTitle</title>
  <environment names="Development">
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" rel="external nofollow" />
    <link rel="stylesheet" href="~/css/site.css" rel="external nofollow" />
  </environment>
  <environment names="Staging,Production">
    <link rel="stylesheet"  rel="external nofollow" 
       asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css" rel="external nofollow" 
       asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
    <link rel="stylesheet" href="~/css/site.min.css" rel="external nofollow" asp-append-version="true" />
  </environment>
</head>
...
</html>

5、參考材料

ASP.NET 5 中的依賴注入(Gunnar Peipman)
ASP.NET Core:使用視圖注入(Gunnar Peipman)

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

相關(guān)文章

最新評論

揭东县| 丰顺县| 韩城市| 平山县| 乡城县| 山东省| 陇川县| 滕州市| 鸡西市| 裕民县| 东乡族自治县| 伽师县| 潼关县| 葵青区| 承德市| 吴川市| 岢岚县| 隆化县| 光山县| 集安市| 临沭县| 西安市| 天等县| 福海县| 冷水江市| 昌江| 宣化县| 安龙县| 忻城县| 大丰市| 夏河县| 光泽县| 马山县| 伊通| 武穴市| 昌黎县| 大冶市| 曲水县| 定结县| 昌乐县| 美姑县|