Dependency injection configurations into views in asp.net core
本文展示如何在ASP.NET Core MVC Application Razor视图中注入和使用应用程序的配置信息。
将配置信息添加到appsettings.json中:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"AppOptions": {
"Host": "http://oneasp.net"
}
}
创建一个AppOptions类,用来读取配置信息:
public class AppOptions : IOptions<AppOptions>
{
public string Host { get; set; }
public AppOptions Value => this;
}
使用ASP.NET Core默认的DI组件注入配置信息:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<AppOptions>(Configuration.GetSection("AppOptions"));
}
在Razor视图中获取配置信息:
@{
ViewData["Title"] = "Home Page";
}
@using Microsoft.Extensions.Options;
@inject IOptions<AppOptions> appOptions;
<h1>@appOptions.Value.Host</h1>

Dependency injection configurations into views in asp.net core的更多相关文章
- ASP.NET Core 十种方式扩展你的 Views
原文地址:http://asp.net-hacker.rocks/2016/02/18/extending-razor-views.html 作者:Jürgen Gutsch 翻译:杨晓东(Savor ...
- Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core --- ...
- Using MongoDB with Web API and ASP.NET Core
MongoDB is a NoSQL document-oriented database that allows you to define JSON based documents which a ...
- #ASP.NET Core 1.0 Key Features
Cross platform support and flexible runtime engine(跨平台支持和灵活的运行时引擎) ASP.NET Core 1.0 offers support f ...
- ASP.NET Core 2 学习笔记(十)视图
ASP.NET Core MVC中的Views是负责网页显示,将数据一并渲染至UI包含HTML.CSS等.并能痛过Razor语法在*.cshtml中写渲染画面的程序逻辑.本篇将介绍ASP.NET Co ...
- ASP.NET Core MVC 2.x 全面教程_汇总贴
Reshaper快捷键盘 快速生成属性:prop Ctrl+. ASP.NET Core MVC 2.x 全面教程:https://www.bilibili.com/video/av38392956 ...
- C# Ioc ASP.NET MVC Dependency Injection
ASP.NET MVC Dependency Injection 同志们,非常快速的Ioc注册接口和注入Mvc Controller,步骤如下: 安装Unity.Mvc NuGet Package 在 ...
- Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)
原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...
- Dependency Injection in ASP.NET Web API 2
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
随机推荐
- Educational Codeforces Round 59
B. Digital root 题意: 题目定义了x的digital root是S(x).S(5)=5,S(38)=S(3+8=11)=S(1+1+2)=2. 有n个询问,每次询问给出ki和xi,要你 ...
- mongo通信协议
先是一个包头: struct MsgHeader { int32 messageLength; // total message size, including this int32 requestI ...
- C#控制台自定义背景颜色,字体颜色大全
效果: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- maven的pom.xml样例
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- OAuth 2.0 学习
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...
- 如何在c语言中源文件调用另一个源文件的函数
在源文件A1.c中调用A2.c 中的函数有两种方法: 1.在A2.c中有完整的函数定义,在A1.c中添加一下要用到的函数原型(声明)就可以了,例如:在A2.c中:有函数void A2(){...};在 ...
- Netty 源码 NioEventLoop(一)初始化
Netty 源码 NioEventLoop(一)初始化 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) Netty 基于事件 ...
- HBase Filter程序样例及Shell(图)
==过滤器执行流程== reset() : reset the filter state before filtering a new row. filterAllRemaining(): true ...
- windows10 装linux子系统
http://blog.csdn.net/Yuxin_Liu/article/details/52347898 试了一下,下载太慢,就没继续用,可以用实验楼这个网来玩玩linux
- Crash以及报错总结
CoreData: Cannot load NSManagedObjectModel.nil is an illegal URL parameter 这是因为在工程中CoreData的命名和AppDe ...