ASP.NET Core中的Startup類

ASP.NET Core程序要求有一個啓動類。按照慣例,啓動類的名字是 "Startup" 。Startup類負責配置請求管道,處理應用程序的全部請求。你能夠指定在Main方法中使用UseStartup<TStartup>()來指定它的名字。啓動類必須包含Configure方法。ConfigureServices方法是可選的。在應用程序啓動的時候它們會被調用。html

 

1、Configure方法 app

用於指定ASP.NET程序如何應答HTTP請求。它經過添加中間件來配置請求管道到一個IApplicationBuilder實例。IApplicationBuilder實例是由依賴注入提供的。ide

示例:網站

在下面默認的網站模板中,一些拓展方法被用於配置管道,支持BrowserLink,錯誤頁,靜態文件,ASP.NET MVC,和Identity。ui

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

{ code

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

loggerFactory.AddDebug(); 中間件

if (env.IsDevelopment()) blog

{

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?}");

});

}

 

2、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>();

}

3、參考

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

 

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

相關文章
相關標籤/搜索