Startup class in ASP. NET Core, corestartup

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.