mvc_study
*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 28px;
color: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* TABLES
=============================================================================*/
table th {
font-weight: bold;
}
table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}
table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
StudyStartup
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Web.Study.ConfigModel;
using Web.Study.MonsterInterface;
namespace Web.Study.InhertStartup
{
public class StudyStartup
{
protected IConfiguration Configuration { get; set; }
public StudyStartup()
{
}
/// <summary>
/// 用于获取配置信息
/// </summary>
/// <param name="configuration"></param>
public StudyStartup(IConfiguration configuration)
{
Configuration = configuration;
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//注入mvc
services.AddMvc();
//添加Options
services.AddOptions();
//获取配置信息
services.Configure<AppSetting>(Configuration.GetSection(nameof(AppSetting)));
//开启Session功能
services.AddSession(options ={
options.IdleTimeout = TimeSpan.FromMinutes(30);
});
//构建一个默认的serviceProvider处理对象
return services.BuildServiceProvider();
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment environment,
ILoggerFactory loggerFactory,
IHttpContextFactory httpContextFactory,
DiagnosticSource diagnosticSource,
DiagnosticListener diagnosticListener)
{
//当前环境为开发环境
if (environment.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
// 重要: session的注册必须在UseMvc之前,因为MVC里面要用
app.UseSession();
//使用webroot中的静态文件
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Program
public static IWebHost BuildWebHost<T>(string[] args) where T:class
{
IWebHostBuilder webHostBuilder = WebHost.CreateDefaultBuilder(args);
IWebHostBuilder hostBuilder = webHostBuilder.UseStartup<T>();
IWebHost webHost = hostBuilder.Build();
return webHost;
}
code explain
construction
public StudyStartup(IConfiguration configuration)
相关源码
IWebHostBuilder webHostBuilder = WebHost.CreateDefaultBuilder(args);
方法解析
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
return new WebHostBuilder()
.UseKestrel()//Specify Kestrel as the server to be used by the web host.
.UseContentRoot(Directory.GetCurrentDirectory())//采用当前目录作为内容跟目录
//配置信息处理
.ConfigureAppConfiguration((Action<WebHostBuilderContext, IConfigurationBuilder>) ((hostingContext, config) =>
{
//注入运行环境
IHostingEnvironment hostingEnvironment = hostingContext.HostingEnvironment;
//加载配置文件
config.AddJsonFile("appsettings.json", true, true).AddJsonFile(string.Format("appsettings.{0}.json", (object) hostingEnvironment.EnvironmentName), true, true);
//根据运行环境加载相应文件
if (hostingEnvironment.IsDevelopment())
{
Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
if (assembly != (Assembly) null)
config.AddUserSecrets(assembly, true);
}
config.AddEnvironmentVariables();
if (args == null)
return;
config.AddCommandLine(args);
}))
//配置日志信息
.ConfigureLogging((Action<WebHostBuilderContext, ILoggingBuilder>) ((hostingContext, logging) =>
{
logging.AddConfiguration((IConfiguration) hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
}))
.UseIISIntegration()//采用IIS进行发布
.UseDefaultServiceProvider((Action<WebHostBuilderContext, ServiceProviderOptions>) ((context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment()));//采用默认的处理机制
}
即若需要使用startup的有参构造,则需要在configureappconfiguration中进行配置相应处理类
请求走向:
初始:Run --> ConfigureServices --> Configure
配置信息的获取:
<Controller>
protected AppSetting ConfigInfo { get; set; }
//读取配置信息
protected BaseController(IOptions<AppSetting> options) => ConfigInfo = options.Value;
DI注入
1.创建的DI注入在ConfigureServices进行处理
注入方式:
services.AddSingleton(u => new MonsterDIController(new GuestDal()));//指定控制器进行注入
services.AddSingleton<IDal,DefaultDal>();//统一注入 全局共享一个
services.AddTransient<IDal, DefaultDal>();//统一注入 每次使用都不一样,不同的类或不同的方法使用都不一样
services.AddScoped<IDal, EmptyDal>();//统一注入 一次请求共享一个对象
注:
当对同一类型构造传入不同类型对象的注入时,
不考虑注入类型或范围
已最终注入类型为传入对象创建实例
例如:
<Controller>
protected IDal Dal { get; set; }
public MonsterDIController(IDal dal)
{
this.Dal = dal;
}
<ConfigureServices>
如上述一致
<Result>
最终创建实例时,会传入EmptyDal
开启Session功能:
<method --> ConfigureServices>
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);//存储时长
});
<method --> Configure >
// 重要: session的注册必须在UseMvc之前,因为MVC里面要用
app.UseSession();