Startup class in ASP. NET Core, corestartup
ASP. NET Core requires a startup class. By convention, the Startup class name is "Startup ". The Startup class is used to configure the request pipeline and process all requests of the application. You can specify UseStartup <TStartup> () in the Main method to specify its name. The launch class must contain the Configure method. The ConfigureServices method is optional. They are called when the application starts.
I. Configure Method
It is used to specify how ASP. NET programs respond to HTTP requests. It configures the request pipeline to an IApplicationBuilder instance by adding middleware. The IApplicationBuilder instance is provided by dependency injection.
Example:
In the following default website templates, some expansion methods are used to configure pipelines, including BrowserLink, error pages, static files, ASP. net mvc, and Identity.
Public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
LoggerFactory. AddConsole (Configuration. GetSection ("Logging "));
LoggerFactory. AddDebug ();
If (env. IsDevelopment ())
{
App. usemediaexceptionpage ();
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 ?} ");
});
}
Ii. ConfigureService Method
This method is optional, but must be called before the Configure method, because some features need to be added before connecting to the request pipeline. The configuration options are set in this method.
publicvoidConfigureServices(IServiceCollection services)
{
// Add framework services.
Services. AddDbContext <ApplicationDbContext> (options =>
Options. UseSqlServer (Configuration. GetConnectionString ("DefaultConnection ")));
Services. AddIdentity <ApplicationUser, IdentityRole> ()
. AddEntityFrameworkStores <ApplicationDbContext> ()
. Adddefadefatokenproviders ();
Services. AddMvc ();
// Add application services.
Services. AddTransient <IEmailSender, AuthMessageSender> ();
Services. AddTransient <ISmsSender, AuthMessageSender> ();
}
Iii. References
Https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup
Link: http://www.cnblogs.com/liszt/p/6403870.html