For people like me who have not contacted the core, the pit is still more, some of the basic configuration and the previous difference is very big, here to do the record
First, Startup1. Registration Services
//This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {services. Addmvc (); //Services. Addtransient<iuser, user> (); //Services. Addsingleton<iuser> (New User ());Services. Addsingleton<iuser, user>(); //Services. Addscoped<> ();//Scope InjectionServices. Addmemorycache ();//memorycache Cache Injection}
2. Configure the HTTP request pipeline
It sounds very, in fact, is used to deal with the various middleware in our program, it must receive a Iapplicationbuilder parameter, we can manually supplement the Iapplicationbuilder use extension method, Add middleware to configure to meet our needs.
//This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {services. Addmvc (); } //This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); App. Usebrowserlink (); } Else{app. Useexceptionhandler ("/home/error"); } app. Usestaticfiles (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}"); }); }
3. Custom configuration Files
Similar to Web. config
///ihostingenvironment Get environment variable information, yes, get environment variable PublicStartup (ihostingenvironment env) {//The Configurationbuilder is created here to load Congfig and other configuration files varBuilder =NewConfigurationbuilder ()//Env. Contentrootpath: Gets the following path for the current project . Setbasepath (env. Contentrootpath)//using the Addjsonfile method to load the Appsettings.json configuration file in the project, the following reloadonchange as the name implies that the file is reloaded if changed. Addjsonfile ("Appsettings.json", Optional:false, Reloadonchange:true) . Addjsonfile ($"appsettings. {env. Environmentname}.json", Optional:true) . Addenvironmentvariables (); //This returns a configuration file followed by a node: IconfigurationrootConfiguration =Builder. Build (); }
Ii. use of logs
Public classHomecontroller:controller { PublicHomeController () {iloggerfactory loggerfactory=Newloggerfactory (). Addconsole (). Adddebug (); _logger= loggerfactory.createlogger(); _logger. Loginformation ("================"); _logger. Loginformation ("LOGGER is START"); _logger. Loginformation ("================"); }}
ASP. NET core Learning two rookie step on the pit