ASP. NET core Learning two rookie step on the pit

Source: Internet
Author: User

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

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.