ASP.NET Core 应用使用Startup类来作为启动类。
 
  Startup类中包含了ConfigureServices方法,Configure方法,IConfiguration,IHostingEnvironment,IServiceCollection,Startup 筛选器
 
  1.  IConfiguration  用于获取配置文件
  1. Configuration.GetConnectionString("MovieContext")

  2. IHostingEnvironment 获取项目环境变量

  1. public Startup(IConfiguration configuration, IHostingEnvironment env)
  2. {
  3. Configuration = configuration;
  4. HostingEnvironment = env;
  5. }
  6. public IHostingEnvironment HostingEnvironment { get; }
  7. public IConfiguration Configuration { get; }
  1. // This method gets called by the runtime. Use this method to add services to the container.
  2. public void ConfigureServices(IServiceCollection services)
  3. {
  4. var contentRootPath = HostingEnvironment.ContentRootPath;
  5. var applicationName = HostingEnvironment.ApplicationName;
  6. services.AddMvc();
  7. }

  3. IServiceCollection 是 DependencyInjection 的一个接口,它NET Core 内置的 依赖注入服务,用于注册服务。

   来自using Microsoft.Extensions.DependencyInjection; 命名空间。

  1. // This method gets called by the runtime. Use this method to add services to the container.
  2. public void ConfigureServices(IServiceCollection services)
  3. {
  4. //注册MovieContext访问上下文类,这样可以在控制器中使用构造函数方式注入使用
  5. services.AddDbContext<MovieContext>(options =>
  6. options.UseMySQL(Configuration.GetConnectionString("MovieContext")));
  7. services.AddTransient<IStartupFilter, RequestSetOptionsStartupFilter>();
  8. services.AddMvc();
  9. }

  4. ConfigureServices 方法就是用于注册服务的,如上面代码 

    • 可选。
    • 在 Configure 方法配置应用服务之前,由 Web 主机调用。
    • 其中按常规设置配置选项。
  将服务添加到服务容器,使其在应用和 Configure 方法中可用。 这些服务通过依赖关系注入或 IApplicationBuilder.ApplicationServices 解析。
 

  5. Configure 方法

    Configure 方法用于指定应用响应 HTTP 请求的方式。 
    可通过将中间件组件添加到 IApplicationBuilder 实例来配置请求管道。 
    Configure 方法可使用 IApplicationBuilder,但未在服务容器中注册。
      承载创建 IApplicationBuilder 并将其直接传递给 Configure 。
    ASP.NET Core 模板配置支持开发人员异常页、BrowserLink、错误页、静态文件和 ASP.NET MVC 的管道,路由
  1. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  2. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  3. {
  4. if (env.IsDevelopment())
  5. {
  6. app.UseDeveloperExceptionPage();
  7. app.UseBrowserLink();
  8. }
  9. else
  10. {
  11. app.UseExceptionHandler("/Home/Error");
  12. }
  13. app.UseStaticFiles();
  14. app.UseMvc(routes =>
  15. {
  16. routes.MapRoute(
  17. name: "default",
  18. template: "{controller=Home}/{action=Index}/{id?}");
  19. });
  20. }
  6. Startup 筛选器
    

    在应用的 Configure 中间件管道的开头或末尾使用 IStartupFilter 来配置中间件。 IStartupFilter 有助于确保中间件在应用请求处理管道的开始或结束时由库添加的中间件之前或之后运行。
    IStartupFilter 实现单个方法(即 Configure),该方法接收并返回 Action<IApplicationBuilder>。 IApplicationBuilder 定义用于配置应用请求管道的类。 有关详细信息,请参阅使用 IApplicationBuilder 创建中间件管道。
    在请求管道中,每个 IStartupFilter 实现一个或多个中间件。 筛选器按照添加到服务容器的顺序调用。 筛选器可在将控件传递给下一个筛选器之前或之后添加中间件,从而附加到应用管道的开头或末尾。
  
  

  1. public class AppOptions
  2. {
  3. public string Option { get; set; } = "Option Default Value";
  4. }
  1. public class RequestSetOptionsMiddleware
  2. {
  3. private readonly RequestDelegate _next;
  4. private IOptions<AppOptions> _injectedOptions;
  5.  
  6. public RequestSetOptionsMiddleware(
  7. RequestDelegate next, IOptions<AppOptions> injectedOptions)
  8. {
  9. _next = next;
  10. _injectedOptions = injectedOptions;
  11. }
  12.  
  13. public async Task Invoke(HttpContext httpContext)
  14. {
  15. Console.WriteLine("RequestSetOptionsMiddleware.Invoke");
  16.  
  17. var option = httpContext.Request.Query["option"];
  18.  
  19. if (!string.IsNullOrWhiteSpace(option))
  20. {
  21. _injectedOptions.Value.Option = WebUtility.HtmlEncode(option);
  22. }
  23.  
  24. await _next(httpContext);
  25. }
  26. }
  1. public class RequestSetOptionsStartupFilter : IStartupFilter
  2. {
  3. public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
  4. {
  5. return builder =>
  6. {
  7. builder.UseMiddleware<RequestSetOptionsMiddleware>();
  8. next(builder);
  9. };
  10. }
  11. }
注册:
  1. // This method gets called by the runtime. Use this method to add services to the container.
  2. public void ConfigureServices(IServiceCollection services)
  3. {
  4. services.AddTransient<IStartupFilter, RequestSetOptionsStartupFilter>();
  5. services.AddMvc();
  6. }
当提供 option 的查询字符串参数时,中间件在 MVC 中间件呈现响应之前处理分配值:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ASP.NET Core 中的应用程序启动 Startup的更多相关文章

  1. ASP.NET Core中实现单体程序的事件发布/订阅

    标题:ASP.NET Core中实现单体程序的事件发布/订阅 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/10468058.html 项目源代码: ...

  2. Asp.Net Core 中获取应用程序物理路径(Getting the Web Root Path and the Content Root Path in ASP.NET Core)

    如果要得到传统的ASP.Net应用程序中的相对路径或虚拟路径对应的服务器物理路径,只需要使用使用Server.MapPath()方法来取得Asp.Net根目录的物理路径,如下所示: // Classi ...

  3. ASP.NET Core 中使用 Hangfire 定时启动 Scrapyd 爬虫

    用 Scrapy 做好的爬虫使用 Scrapyd 来管理发布启动等工作,每次手动执行也很繁琐;考虑可以使用 Hangfire 集成在 web 工程里. Scrapyd 中启动爬虫的请求如下: curl ...

  4. 探索ASP.Net Core 3.0系列六:ASP.NET Core 3.0新特性启动信息中的结构化日志

    前言:在本文中,我将聊聊在ASP.NET Core 3.0中细小的变化——启动时记录消息的方式进行小的更改. 现在,ASP.NET Core不再将消息直接记录到控制台,而是正确使用了logging 基 ...

  5. ASP.NET Core 中文文档 第三章 原理(1)应用程序启动

    原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...

  6. [08]ASP.NET Core 中 launchsettings.json 启动配置文件

    ASP.NET Core launchsettings.json 启动配置文件 本文作者:梁桐铭- 微软最有价值专家(Microsoft MVP) 文章会随着版本进行更新,关注我获取最新版本 本文出自 ...

  7. 在docker中运行ASP.NET Core Web API应用程序

    本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过程中,也会对docker的使用进行一些简单的描述.对于.NET Cor ...

  8. 在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序

    前言 Visual Studio 2017已经发布了很久了.做为集成了Asp.Net Core 1.1的地表最强IDE工具,越来越受.NET系的开发人员追捧. 随着Google Angular4的发布 ...

  9. 【Asp.Net Core】在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序

    前言 Visual Studio 2017已经发布了很久了.做为集成了Asp.Net Core 1.1的地表最强IDE工具,越来越受.NET系的开发人员追捧. 随着Google Angular4的发布 ...

随机推荐

  1. 【转】Java中的内部类和匿名类

       Java内部类(Inner Class),类似的概念在C++里也有,那就是嵌套类(Nested Class),乍看上去内部类似乎有些多余,它的用处对于初学者来说可能并不是那么显著,但是随着对它的 ...

  2. solr精确查询,查询关键字分词后,指定满足匹配所有

    一.solr查询,查询配置了查询分词器的字段,默认会对查询关键字做分词处理 1.如查询关键字F1501ZY000011,使用solr7自带的中文分词器,默认会分词为:f,1501,zy,000011 ...

  3. Vue基础知识之vue-resource和axios(三)

    vue-resource Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的DOM选择器,就没有必要引入jQuery.vue-resource是Vue.js的 ...

  4. Python——截取web网页长图

    # -*- coding: utf8 -*-import timeimport xlrdfrom selenium import webdriver def read_excel(filename): ...

  5. 常用的ubantu操作命令

    Ubuntu软件操作的相关命令 sudo apt-get update 更新源 sudo apt-get install package 安装包 sudo apt-get remove package ...

  6. 好记性不如烂笔头--linux学习笔记9练手写个shell脚本

    #!/bin/bash #auto make install httpd #by authors baker95935 #httpd define path variable H_FILES=http ...

  7. Git----时光穿梭机之工作区和暂存区03

    Git和其他版本控制系统SVN的一个不同之处就是有暂存区的概念 先来看看名词解释 工作区(Working Directory) 就是你在我电脑里能看到的目录,比如我的learngittest文件夹就是 ...

  8. Maven编译并打包Mahout CDH版源码

    目录 1. 问题描述 最近在使用Mahout里的推荐算法进行实验,由于业务需求,需要修改Mahout源码,将原本输出到HDFS上的结果输出到HBase中.由于Mahout发布的源码都是Maven项目, ...

  9. hive中修改序列化格式分隔符

    标签: hadoophivealtertable 2014-11-19 10:45 4219人阅读 评论(0) 收藏 举报  分类: hadoop(6)  版权声明:本文为博主原创文章,未经博主允许不 ...

  10. 磁盘存储结构与文件恢复实验(FAT文件系统)

    实验地点:主楼A2-412 一.实验室名称:主楼实验室A2-412                  二.实验项目名称:磁盘存储结构与文件恢复实验 三.实验学时:6学时 四.实验原理: 在Debug环 ...