ASP. NET 5 getting started -- Application Startup, asp. netstartup

Source: Internet
Author: User

ASP. NET 5 getting started -- Application Startup, asp. netstartup
ASP. NET 5 getting started -- Application Startup class Startup ¶

In ASP. NET 5,StartupClass is the entry point of an application. We can configure different content for different environments.

The compiler will find all*.csFile, and the runtime will look for the class names under all namespacesStartupAs the startup method.

Annotation

You can setproject.jsonSelect the files and folders to be compiled (or not required). You can also search for files in different programming sets.StartupClass.

StartupClass must defineConfigureMethod, you can also defineConfigureServicesMethod.

ConfigureServices method ingress ¶

ConfigureServicesDefines the services we use, such as MVC, EF, Identity, Logging, and Route. You can also customize some services.

In the following example, the EntityFramework (for data accessconfig.jsonCorrectly set the connection string), Identity (for Identity Authentication/login), and MVC.

public void ConfigureServices(IServiceCollection services){    // Add framework services.    services.AddEntityFramework()        .AddSqlServer()        .AddDbContext<ApplicationDbContext>(options =>            options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));    services.AddIdentity<ApplicationUser, IdentityRole>()        .AddEntityFrameworkStores<ApplicationDbContext>()        .AddDefaultTokenProviders();    services.AddMvc();    // Add application services.    services.AddTransient<IEmailSender, AuthMessageSender>();    services.AddTransient<ISmsSender, AuthMessageSender>();}

After callingConfigureServicesThen, it is calledConfigureMethod.

Configure method ingress ¶

ConfigureThe method signature must containIApplicationBuilderCan also contain some other WufuIHostingEnvironmentAndILoggerFactory.

Public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {// output log loggerFactory in the console. addConsole (Configuration. getSection ("Logging"); loggerFactory. addDebug (); // In the development environment, the error details if (env. isDevelopment () {app. useBrowserLink (); app. usemediaexceptionpage (); app. useDatabaseErrorPage ();} else {// otherwise, direct the error page app. useExceptionHandler ("/Home/Error"); // create data Library try {using (var servicelist = app. applicationServices. getRequiredService <IServiceScopeFactory> (). createid () {serviceid. serviceProvider. getService <ApplicationDbContext> (). database. migrate () ;}} catch {}} app. useIISPlatformHandler (options => options. authenticationDescriptions. clear (); // allow access to the static file app in the wwwroot folder. useStaticFiles (); // sets the authentication method app. useIdentity (); // sets the MVC routing app. useMvc (r Outes => {routes. MapRoute (name: "default", template: "{controller = Home}/{action = Index}/{id ?} ");});}

Warning

The MVC routing method must beConfigure, Only inConfigureServicesIs invalid.

Related Article

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.