ASP. NET 5 getting started -- Application Startup, asp. netstartup
ASP. NET 5 getting started -- Application Startup class Startup ¶
In ASP. NET 5,Startup
Class is the entry point of an application. We can configure different content for different environments.
The compiler will find all*.cs
File, and the runtime will look for the class names under all namespacesStartup
As the startup method.
Annotation
You can setproject.json
Select the files and folders to be compiled (or not required). You can also search for files in different programming sets.Startup
Class.
Startup
Class must defineConfigure
Method, you can also defineConfigureServices
Method.
ConfigureServices method ingress ¶
ConfigureServices
Defines 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.json
Correctly 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 callingConfigureServices
Then, it is calledConfigure
Method.
Configure method ingress ¶
Configure
The method signature must containIApplicationBuilder
Can also contain some other WufuIHostingEnvironment
AndILoggerFactory
.
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 inConfigureServices
Is invalid.