• 这一节介绍Options方法,继续在OptionsBindSample项目下。
  • 在项目中添加一个Controllers文件夹,文件夹添加一个HomeController控制器
  • HomeController.cs
  •          private readonly Class _myClass;
    
             public HomeController(IOptions<Class> classAccesser)//IOptions的方法注入
    {
    this._myClass = classAccesser.Value;
    } public IActionResult Index()
    {
    return View(_myClass);
    }
  • 在项目中添加一个Views文件夹,文件夹中添加一个Home文件夹(这和ASP.NET MVC一样),Home文件夹下添加一个Index.cshtml
  •  @model OptionsBindSample.Class
    @{
    ViewData["Title"] = "Index";
    } <h2>Index</h2> <div>
    @foreach(var stu in Model.Students)
    {
    <span>Name:@stu.Name</span>
    <span>Age:@stu.Age</span> }
    </div>
  • 此时StartUp.cs应该改成这样
  •          public IConfiguration Configuration { get; set; }
    
             public Startup(IConfiguration configuration)
    {
    this.Configuration = configuration;
    } // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
    services.Configure<Class>(Configuration); services.AddMvc();
    } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    } app.UseMvcWithDefaultRoute();
    }

也可以不用在Controller中依赖注入,而在视图中注入,改成如下:

HomeController.cs

         public IActionResult Index()
{
3 return View();
}

Index.cshtml:

 @using Microsoft.Extensions.Options
@inject IOptions<OptionsBindSample.Class> ClassAccesser
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2> <div>
@foreach(var stu in ClassAccesser.Value.Students)
{
<span>Name:@stu.Name</span>
<span>Age:@stu.Age</span> }
</div>

(十三)在ASP.NET CORE中使用Options的更多相关文章

  1. (13)ASP.NET Core 中的选项模式(Options)

    1.前言 选项(Options)模式是对配置(Configuration)的功能的延伸.在12章(ASP.NET Core中的配置二)Configuration中有介绍过该功能(绑定到实体类.绑定至对 ...

  2. ASP.NET Core 中的那些认证中间件及一些重要知识点

    前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...

  3. ASP.NET Core 中文文档 第三章 原理(1)应用程序启动

    原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...

  4. ASP.NET Core 中文文档 第三章 原理(6)全球化与本地化

    原文:Globalization and localization 作者:Rick Anderson.Damien Bowden.Bart Calixto.Nadeem Afana 翻译:谢炀(Kil ...

  5. ASP.NET Core 中文文档 第三章 原理(13)管理应用程序状态

    原文:Managing Application State 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:高嵩 在 ASP.NET Core 中,有多种途径可以对应用程序的状态进行 ...

  6. [转]ASP.NET Core 中的那些认证中间件及一些重要知识点

    本文转自:http://www.qingruanit.net/c_all/article_6645.html 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系 ...

  7. 如何在ASP.NET Core中实现CORS跨域

    注:下载本文的完整代码示例请访问 > How to enable CORS(Cross-origin resource sharing) in ASP.NET Core 如何在ASP.NET C ...

  8. ASP.NET Core 中文文档

    ASP.NET Core 中文文档 翻译计划 五月中旬 .NET Core RC2 如期发布,我们遂决定翻译 ASP.NET Core 文档.我们在 何镇汐先生. 悲梦先生. 张仁建先生和 雷欧纳德先 ...

  9. 在ASP.NET Core 中使用Cookie中间件

    在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...

随机推荐

  1. 校验文件是否是同一个文件,以及mac中使用MD5命令

    背景 sz了war包,因为查看不到里面的内容,并不确定是否是同一个文件. 解决 通过MD5校验 md5sum xxxx 但是在mac中是没有这个命令的下载半天没下载下来,下面是快捷操作. 1.打开终端 ...

  2. testNG官方文档翻译-3 testng.xml

    你可以通过以下几种不同的方法触发TestNG: 用一个testng.xml文件 使用ant 从命令行触发 这个章节将会介绍testng.xml的格式(你也可以在下面找到关于ant和命令行的内容). 关 ...

  3. 二维RMQ hdu 2888

    题目:点这里 题意:给出一个n*m的矩阵,然后又Q个询问:每个询问有x1,y1,x2,y2,x1,y1为子矩阵的左上角坐标,x2,y2为右上角的坐标.求此子矩阵中元素最大值,判断最大值是否在子矩阵四个 ...

  4. C#获取系统服务+进程+启动时间

    原文:C#获取系统服务+进程+启动时间 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/weixin_38208401/article/details ...

  5. react-router v4 理解

    1.Router (1)最基础的路由器,必须有history属性 (2)BrowserRouter和HashRouter都是由Router组件扩展而来 2.BrowserRouter (1)Brows ...

  6. C#编程入门--MYSQLHELPER

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  7. table 表头不动,tbody滚动对齐

    http://www.imaputz.com/cssStuff/bigFourVersion.html# https://blog.csdn.net/yiifaa/article/details/52 ...

  8. 使用Fiddler抓取手机包

    配置Fiddler 设置抓取HTTPS包 允许为外部连接 配置移动端 移动端需要能够连接到主机做代理, 设置移动端的网络, 端口为Fiddler的端口, 然后给移动端安装证书, 访问主机名+代理端口号 ...

  9. AbstractQueuedSynchronizer 详解

    package java.util.concurrent.locks; 基本介绍 AbstractQueuedSynchronizer(队列同步器)可以看作是并发包(java.util.concurr ...

  10. 【Http】keepalive

    http是现在web领域极其普遍的应用层传输协议, 目前常见的使用版本则是http1.1, 当然最先版本是http2.0. 传统的Http应用里都是一次TCP连接一次request.   image ...