ASP.NET Core 启动过程(基础) 电脑版发表于:2020/5/6 11:34 ![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#ASP.NET Core 启动过程 [TOC] <br/> 环境准备 ------------ <br/> >###项目结构 ![实验准备](https://img.tnblog.net/arcimg/hb/c0039700bc8a4a2ea9359e01fbff76df.png "实验准备") >###项目环境 <font style="color:#3498db;font-weight:bold;">.netcore3.1</font> <br/> 开始 ------------ <br/> >###启动流程 ![启动顺序](https://img.tnblog.net/arcimg/hb/d36dab61db144ec98518ddf6dba9e39b.png "启动顺序") >###修改项目 <br/> >修改 <font style="color:#2ecc71;font-weight:bold;">Program.cs</font> ```csharp public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { Console.WriteLine("Configure WebHost Defaults"); webBuilder.UseStartup<Startup>(); }) .ConfigureHostConfiguration(builder => { Console.WriteLine("Configure Host Configuration"); }) .ConfigureServices(service => { Console.WriteLine("Configure Services"); }) .ConfigureAppConfiguration(builder => { Console.WriteLine("Configure App Configuration"); }); } ``` >修改 <font style="color:#2ecc71;font-weight:bold;">Startup.cs</font> ```csharp public class Startup { public Startup(IConfiguration configuration) { Console.WriteLine("Startup"); Configuration = configuration; } 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) { Console.WriteLine("Startup - Configure Services"); services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { Console.WriteLine("Startup - Configure"); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } ``` >###结果 ![结果](https://img.tnblog.net/arcimg/hb/1329d807303d4a65a2224070d54d9ddb.png "结果") >###不通过Startup进行注册 <br/> >对 <font style="color:#2ecc71;font-weight:bold;">Program.cs</font> 稍作修改 ```csharp public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { Console.WriteLine("Configure WebHost Defaults"); //webBuilder.UseStartup<Startup>(); webBuilder.ConfigureServices(services => { Console.WriteLine("webBuilder - Configure Services"); services.AddControllers(); }); webBuilder.Configure(app => { Console.WriteLine("webBuilder - Configure"); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }); }) .ConfigureHostConfiguration(builder => { Console.WriteLine("Configure Host Configuration"); }) .ConfigureServices(service => { Console.WriteLine("Configure Services"); }) .ConfigureAppConfiguration(builder => { Console.WriteLine("Configure App Configuration"); }); } ``` >结果 ![结果](https://img.tnblog.net/arcimg/hb/53aa9881b55746a5a701461dff26e2fb.png "结果") <br/> 项目地址 ------------ <br/> >GitHub: https://github.com/AiDaShi/GeekTimeLearning <br/> 课程推荐 ------------ <br/> ![](https://img.tnblog.net/arcimg/hb/6bb0a226374d487bb01aa9c586a1a9ff.jpeg)