默认的网站构建方式

VS2015新建asp.net core项目,项目建立完成后,有两个文件,Program.csStartup.cs.

    public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}

从入口函数开始,WebHost构建网站时调用了Startup类。再来看Startup.cs文件

    public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} //Startup构造函数只由asp.net core框架调用第一个,所以下面这个构造方法不会执行。
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); Configuration = builder.Build();
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
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.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
} app.UseStaticFiles(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}

其中,第二个构造函数是我自己加的,VS默认生成的是第一个构造函数。跟踪调试,第一个构造函数已经有configuration对象传入进来,并且已经加载了配置信息。没有去翻asp.net core的源码,但是在文档中发现了这样一句话:

Remarks

The following defaults are applied to the returned WebHostBuilder: use Kestrel as the web server, set the ContentRootPath to the result of GetCurrentDirectory(), load IConfiguration from 'appsettings.json' and 'appsettings.[EnvironmentName].json', load IConfiguration from User Secrets when EnvironmentName is 'Development' using the entry assembly, load IConfiguration from environment variables, load IConfiguration from supplied command line args, configures the ILoggerFactory to log to the console and debug output, enables IIS integration, enables the ability for frameworks to bind their options to their default configuration sections, and adds the developer exception page when EnvironmentName is 'Development'
查看原文

原来,WebHostBuilder在默认情况下,做了以下几件事:

  • 使用了Kestrel作为Web服务器
  • 加载appsettings.json和appsettings.[EnvironmentName].json
  • 当Development环境下,会从User Secrets中加载用户私有数据
  • 从环境变量中加载数据
  • 从命令行参数中加载数据
  • 加载控制台为日志输出
  • 启用IIS integration
  • ....

正是由于WebHostBuilder帮助我们做了这么多处理,我们的网站才能够启动。

如何覆盖默认的构建方式

如上所述,WebHostBuilder帮我们完成了启动过程,那我们如何自定义我们的网站构建过程呢。
看上面代码中Startup.cs中的第二个构造函数,WebHostBuilder只会调用第一个构造函数,所以我们如果需要自定义,则需要把第二个构造函数放到最前。
这样我们就可以配置我们的配置文件加载方式、Web服务器等。

Asp.net Core Startup Class中是如何获取配置信息的的更多相关文章

  1. ASP.NET Core 1.0 中使用 Log 日志配置

    https://github.com/aspnet/Logging https://docs.asp.net/en/latest/fundamentals/logging.html ASP.NET C ...

  2. 如何在ASP.NET Core Web API中使用Mini Profiler

    原文如何在ASP.NET Core Web API中使用Mini Profiler 由Anuraj发表于2019年11月25日星期一阅读时间:1分钟 ASPNETCoreMiniProfiler 这篇 ...

  3. .net core 2.0 mvc 获取配置信息

    mvc_core_config *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 ...

  4. 探索ASP.Net Core 3.0系列二:聊聊ASP.Net Core 3.0 中的Startup.cs

    原文:探索ASP.Net Core 3.0系列二:聊聊ASP.Net Core 3.0 中的Startup.cs 前言:.NET Core 3.0 SDK包含比以前版本更多的现成模板. 在本文中,我将 ...

  5. ASP.NET Core HTTP 管道中的那些事儿

    前言 马上2016年就要过去了,时间可是真快啊. 上次写完 Identity 系列之后,反响还不错,所以本来打算写一个 ASP.NET Core 中间件系列的,但是中间遇到了很多事情.首先是 NPOI ...

  6. ASP.NET Core 1.0 中使用 Swagger 生成文档

    github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...

  7. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  8. [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了

    [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 本文首发自:博客园 文章地址: https://www.cnblogs.com/yilezhu/p/ ...

  9. ASP.NET Core 2.2中的Endpoint路由

    Endpoint路由 在ASP.NET Core 2.2中,新增了一种路由,叫做Endpoint(终结点)路由.本文将以往的路由系统称为传统路由. 本文通过源码的方式介绍传统路由和Endpoint路由 ...

随机推荐

  1. matlab中的knn函数

    knn 最邻近分类 Class = knnclassify(test_data,train_data,train_label, k, distance, rule) k:选择最邻近的数量 distan ...

  2. redis下的持久化保存

    rdb.h   rdb.c  --->  完成数据保存到临时文件,再利用rename保存到指定文件的过程: 如果需要写一个数据持久化保存的功能时,可以参考这部分代码: //rdb API int ...

  3. 剖析php脚本的超时机制

    在做php开发的时候,经常会设置max_input_time.max_execution_time,用来控制脚本的超时时间.但却从来没有思考过背后的原理. 趁着这两天有空,研究一下这个问题.文中源码取 ...

  4. [SHOI2010]最小生成树

    题目 首先让其余所有边都减\(1\)和让自己加\(1\)没什么区别 考虑\(kruskal\)的过程 首先边权大于这条边的是不用考虑的 考虑把那些边权比这条边小的调节到比这条边大,这样就相当于在生成树 ...

  5. [Python 多线程] Timer定时器/延迟执行、Event事件 (七)

    Timer继承子Thread类,是Thread的子类,也是线程类,具有线程的能力和特征.这个类用来定义多久执行一个函数. 它的实例是能够延迟执行目标函数的线程,在真正执行目标函数之前,都可以cance ...

  6. Mysql 创建普通用户、数据库、表、插入记录,用户赋权

    C:\phpStudy\MySQL\bin>mysql -uroot -proot -h127.0.0.1 //创建用户 mysql> insert into mysql.user (ho ...

  7. PHP面试系列 之Linux(四)---- Shell脚本

    一.脚本执行方式 1.先赋予权限 chmod +x test.sh; 再直接执行 ./test.sh 2.调用解释器使得脚本执行 3.使用source命令 source test.sh 二.编写基础 ...

  8. 利用maven开发springMVC项目(二)——框架配置

    申明:主要内容来源于大神博客(使用IntelliJ IDEA开发SpringMVC网站(二)框架配置),我只是用eclipse自己练习使用,记录下来也只是为了学习使用,没有任何的商业用途,侵权必删. ...

  9. ubuntu 14.04 将窗体button移到右边

    刚刚安装了Ubuntu 14.04,想改动窗体button的位置.但依照曾经的办法发现不行了,在gconftool-->apps中找不到metacity. 多方查找后找到解决方式,例如以下 Ub ...

  10. 一些有用的社区论坛,wiki网站(持续更新)

    1. IBM开发者(IBM developerwork): 这是一个比较全面的网站,上面有关于linux 管理.linux内核设计.存储等各个方面的知识,内容广泛,参考价值很高 http://www. ...