ASP.NET Core 基础知识(四) Startup.cs类

ASP.NET Core应用程序需要一个启动类,按照约定命名为Startup在 Program 类的主机生成器上调用 Build 时,将生成应用的主机, 通常通过在主机生成器上调用 WebHostBuilderExtensions.UseStartup<TStartup> 方法来指定 Startup 类。您可以为不同的环境定义不同的Startup类,并在运行时选择适当的Startup类。通俗的讲,ASP.NET Core应用程序启动的时候将会根据当前的运行环境(生产环境(Production)或者开发环境(Development))自动选择启动类。比如在一个ASP.NET Core应用程序中,具有两个启动类StartupStartupDevelopment,那么当我们的启动环境设置为开发环境的时候,启动时将会搜索启动程序集,优先使用StartupDevelopment这个带有Development后缀的启动类。具体将在ASP.NET Core 基础知识(十一)环境介绍,这里只介绍Startup.cs的作用。 

 

startup.cs的默认代码如下

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            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)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // 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();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }

 

Startup类有两个方法ConfigureServices(可选)和Configure(必选),主机提供 Startup 类构造函数可用的某些服务。 应用通过 ConfigureServices 添加其他服务。 然后,主机和应用服务都可以在 Configure 和整个应用中使用。

1、ConfigureServices 方法介绍

  (1)特点

 

  (2)作用:将服务添加到服务容器,使其在应用和 Configure 方法中可用。 服务通过依赖关系注入或 ApplicationServices 进行解析。

  

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    services.AddDefaultIdentity<IdentityUser>()
        .AddDefaultUI(UIFramework.Bootstrap4)
        .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}

  

 

2、Configure方法介绍  

  (1)作用:Configure 方法用于指定应用响应 HTTP 请求的方式。 可通过将中间件组件添加到 IApplicationBuilder 实例来配置请求管道。(中间件具体将在ASP.NET Core 基础知识(六)中间件介绍)  

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();

    app.UseMvc();
}

  每个 Use 扩展方法将一个或多个中间件组件添加到请求管道。 例如,UseMvc 扩展方法将路由中间件添加到请求管道,并将 MVC 配置为默认处理程序。

转载于:https://www.cnblogs.com/qtiger/p/11175177.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值