• 这一节介绍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. Gerrit(1): Manage Projects

    1) Register an openid account https://login.ubuntu.com/+login 2) Custom settings set SSH pubkey set ...

  2. 20140506 visio 画布大小 栈实现队列 堆空闲内存地址链表 堆最大可分配的内存 可用内存链表

    1.调整visio的画布大小 按住Ctrl鼠标移动到画布边缘即可 2.两个栈实现一个队列 一个栈用于入队,一个用于出队 #include<iostream> #include<sta ...

  3. linux 编译指定库、头文件的路径问题(转)

    1. 为什么会出现undefined reference to 'xxxxx'错误? 首先这是链接错误,不是编译错误,也就是说如果只有这个错误,说明你的程序源码本身没有问题,是你用编译器编译时参数用得 ...

  4. 2019牛客国庆集训派对day7 A 2016

    链接:https://ac.nowcoder.com/acm/problem/52800来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K ...

  5. lca 倍增模版

    ; void dfs(int u,int fa){ d[u]=d[fa]+; p[u][]=fa; ;i<POW;i++) p[u][i]=p[p[u][i-]][i-]; int sz=edg ...

  6. add new color number to the color drop down in enovia PLM

    Ticket description:A55482L Ticket attachment: Open the attached page 2. Open the internationalizing ...

  7. mysql数据库 --数据类型、约束条件

    今日内容 表的详细使用 1.创建表的完成语法 2.字段类型 整型.浮点型.字符类型.日期类型.枚举与集合类型 3.约束条件 primary key.unique.not null.default 一. ...

  8. JMeter学习篇(一):测试实例讲解

    1.JMeter的下载与安装 Jmeter官方下载地址:http://jmeter.apache.org/download_jmeter.cgi,下载jmeter是一个zip压缩包,解压后,直接运行a ...

  9. 深度探索C++对象模型之第三章:数据语义学

    如下三个类: class X { }: class Y :public virtual X { }; class Z : public virtual X {}; class A :public Y, ...

  10. vue之自定义指令

    1.自定义指令的作用 除了核心功能默认内置的指令 (v-model 和 v-show),Vue 也允许注册自定义指令.注意,在 Vue2.0 中,代码复用和抽象的主要形式是组件.然而,有的情况下,你仍 ...