• 这一节介绍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. JVM 源码分析之 javaagent 原理完全解读

    转载:https://infoq.cn/article/javaagent-illustrated 本文重点讲述 javaagent 的具体实现,因为它面向的是我们 Java 程序员,而且 agent ...

  2. GF学习未解之谜

    1.很奇怪事件管理器里面的用到的订阅事件里面的ID是通过typeof(xxx).GetHashCode()得到的,怎么解决id重复的问题? 2.log系统里面是不是直接全部当做多参数解决问题比较好?

  3. CSS3:FlexBox的详解

    Flexbox是Flexible box 的简称(灵活的盒子容器),是CSS3引入的新的布局模式.它决定了元素如何在页面上排列,使它们能在不同的屏幕尺寸和设备下可预测地展现出来. 它之所以被称为 Fl ...

  4. nth_element函数

    使用方法:nth_element(start, start+n, end) 使第n大元素处于第n位置(从0开始,其位置是下标为n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都 ...

  5. 操作bin目录下的文件

    string dir = AppDomain.CurrentDomain.BaseDirectory + "Video"; if (!System.IO.Directory.Exi ...

  6. Yacc - 一个生成 LALR(1) 文法分析器的程序

    SYNOPSIS 总览 yacc [ -dlrtv ] [ -b file_prefix ] [ -p symbol_prefix ] filename DESCRIPTION 描述 Yacc 从 f ...

  7. PHP72w安装

    PHP72w #  rpm  -Uvh   https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm #  rpm ...

  8. LNMP部署

    部署企业LNMP架构 源码包:nginx-* ; mysql-* ; php-* ; boost-* ; zend-loader-php5.6-linux-* ;yum软件: pcre-devel z ...

  9. set -x 调试shell

    在上面的结果中,前面有“+”号的行是shell脚本实际执行的命令,前面有“++”号的行是执行trap机制中指定的命令,其它的行则是输出信息. shell的执行选项除了可以在启动shell时指定外,亦可 ...

  10. zmq作为守护进程?等待连接

    服务端是作为守护进程在运行的,客户端connect成功,但write时直接退出了,我在想肯能服务端socket在write时已经失效了,不然为什么会出现write时进程退出呢?现在的问题是,我要怎么才 ...