zoukankan      html  css  js  c++  java
  • ASP.NET Core中的Startup类

    ASP.NET Core程序要求有一个启动类。按照惯例,启动类的名字是 "Startup" 。Startup类负责配置请求管道,处理应用程序的所有请求。你可以指定在Main方法中使用UseStartup<TStartup>()来指定它的名字。启动类必须包含Configure方法。ConfigureServices方法是可选的。在应用程序启动的时候它们会被调用。

    一、Configure方法

    用于指定ASP.NET程序如何应答HTTP请求。它通过添加中间件来配置请求管道到一个IApplicationBuilder实例。IApplicationBuilder实例是由依赖注入提供的。

    示例:

    在下面默认的网站模板中,一些拓展方法被用于配置管道,支持BrowserLink,错误页,静态文件,ASP.NET MVC,和Identity。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

    {

    loggerFactory.AddConsole(Configuration.GetSection("Logging"));

    loggerFactory.AddDebug();

    if (env.IsDevelopment())

    {

    app.UseDeveloperExceptionPage();

    app.UseDatabaseErrorPage();

    app.UseBrowserLink();

    }

    else

    {

    app.UseExceptionHandler("/Home/Error");

    }

    app.UseStaticFiles();

    app.UseIdentity();

    app.UseMvc(routes =>

    {

    routes.MapRoute(

    name: "default",

    template: "{controller=Home}/{action=Index}/{id?}");

    });

    }

    二、ConfigureService方法

    这个方法是可选的,但必须在Configure方法之前调用, 因为一些features要在连接到请求管道之前被添加。配置选项是在这个方法中设置的。

    publicvoidConfigureServices(IServiceCollection services)
    {

    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();
    services.AddMvc();

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

    }

    三、参考

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup

    原文链接:http://www.cnblogs.com/liszt/p/6403870.html

  • 相关阅读:
    AWS Dynamodb aggregation的处理问题
    Java线程池的实现与应用
    机器学习笔记之基本框架:
    Azure machine learning series 1 Introduction
    AWS的安全机制
    模板
    矩阵快速幂总结
    中位数总结
    KMP算法
    动态规划----背包总结
  • 原文地址:https://www.cnblogs.com/liszt/p/6403870.html
Copyright © 2011-2022 走看看