.NET Core中Startup.cs文件的作用

我们在建立.net core项目中Startup.cs文件必不可少,最近在面试中别人提问到Startup.cs中有哪三种方法,以及对应的作用是什么,没有答复的很好,因此也总结记录一下。

以前我们在创建.net项目的时候会看到两个文件:

  1. global.asax 文件,可以在启动 Web 应用程序期间编写代码来执行的一个地方
  2. web.config 文件,用来包含应用程序需要执行的所有配置参数

在 ASP.NET Core 中,这些文件全部消失,取而代之的是使用 Startup.cs 加载配置和启动代码

Startup.cs 文件中有一个 Startup 类,在这个类中可以配置应用程序,甚至配置配置源

下面先看看原文件的结构

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection;  namespace HelloWorld {     public class Startup     {         // 该方法在运行时被调用。         // 可以使用该方法将服务添加到容器中         // 更多信息配置应用程序的信息,可以查看 https://go.microsoft.com/fwlink/?LinkID=398940         public void ConfigureServices(IServiceCollection services)         {         }          // 该方法在运行时被调用         // 可以使用该方法来配置 HTTP 请求管道         public void Configure(IApplicationBuilder app, IHostingEnvironment env)         {             if (env.IsDevelopment())             {                 app.UseDeveloperExceptionPage();             }              app.Run(async (context) =>             {                 await context.Response.WriteAsync(Hello World!);             });         }     } } 

Startup类里面必须公开包含两个方法以及一个构造函数:

1、ConfigureServices()

public void ConfigureServices(IServiceCollection services){} 

用于定义应用程序所需要的服务的地方,比如我们所说的注入各种服务就是在这里

2、Configure

public void Configure(IApplicationBuilder app, IHostingEnvironment env){} 

用于定义请求管道中的中间件,app.Run方法设置的地方

3、还有一个是Startup本身的一个构造函数,加上之后有三个方法。

  public Startup()          {              var builder = new ConfigurationBuilder()                 .SetBasePath(Directory.GetCurrentDirectory())                 .AddJsonFile(AppSettings.json);              Configuration = builder.Build();          } 

示例:将HolleWord!输出改成从配置文件中获取内容并输出

using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration;  namespace HelloWorld {     public class Startup     {         public Startup()          {              var builder = new ConfigurationBuilder()                 .SetBasePath(Directory.GetCurrentDirectory())                 .AddJsonFile(AppSettings.json);              Configuration = builder.Build();          }          public IConfiguration Configuration { get; set; }          // This method gets called by the runtime. Use this method to add services to the container.         // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940         public void ConfigureServices(IServiceCollection services)         {         }          // 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.Run(async (context) =>             {                 var msg = Configuration[message];                 await context.Response.WriteAsync(msg);             });         }     } }