• 这一节介绍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. install busybox时报error: storage size of ‘rlimit_fsize’ isn’t known struct rlimit rlimit_fsize

    解决办法: 在busybox根目录下查找到文件:find -name libbb.h 在libbb.h.h中包含sys/resource.h 说明: 上述错误的原因是rlimit结构体未知,原因是相应 ...

  2. IDM自定义报错页面

    由于用户两次重复单点登录会跳转至原生态ORACLE的错误页面页面.请提供配置方法.原因:是由于重复登录导致的.解决方案:Oracle官方给出了具体的解决方案,具体如下:I.创建战争档案a.创建目录&q ...

  3. 2018-2-13-win10-uwp-iot

    title author date CreateTime categories win10 uwp iot lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:2 ...

  4. opencv2.4.9+vs2012安装配置

    需要下载并安装vs2012  http://pan.baidu.com/s/1qXP76CO     第一次启动会提示要求输入激活序列号,请输入:YKCW6-BPFPF-BT8C9-7DCTH-QXG ...

  5. nginx 简单使用

    一,下载 http://nginx.org/en/download.html 这个是我下载的windows版本 二,解压后目录 三,修改配置文件 (由于80端口很可能被 SQL Server Repo ...

  6. Linux系统的buff/cache缓存清理脚本

    cacheClean.sh #!/bin/bash # 日期: # 作者: 黄慧丰/何鹏举 # 说明: fastdfs所在的linux系统的buffer cache过大, 且并没有有效的自动回收, 因 ...

  7. shell 例子

    shell编程入门 http://www.runoob.com/linux/linux-shell-variable.html http://c.biancheng.net/cpp/shell/ .查 ...

  8. QueryList.class.php很方便的一个采集数据工具。

    QueryList.class.php是一个非常方面的网站数据采集工具.可以在这个上面采集别的网站数据. /*调用开始*/ require 'QueryList.class.php';header(' ...

  9. 使用node搭建简单的服务

    //创建依赖模块var http = require('http');var url = require('url');var fs = require('fs');var server = http ...

  10. springMVC快速入门 共分为五步

    springMVC快速入门 共分为5步分别为: 1 导入依赖 ​​ 2 spring-mvc.xml 配置 ​ 3 web.xml配置 ​ 4 自定义一个核心控制类 ​ 5 页面配置 详细步骤以及代码 ...