ASP. NET MVC Core Introduction and Project Interpretation

Source: Internet
Author: User

ASP. NET MVC Core project folder interpretation

I. Overview of Project Folders

1.1, Properties--launchsettings.json

  You can find the file in the "Properties" folder in your project by launching the configuration file. The launchSettings.json file holds unique configuration criteria for an ASP. NET core application for application startup preparation, including environment variables, development ports, and so on. The configuration changes in the file are the same as those launchSettings.json submitted in the developer right-click Project-Properties (the property in the right-click attribute is really very poor), and synchronous updates are supported. This file sets the different environments that Visual Studio can launch, the following is the default code generated by the Launchsettings.json file in the sample project:

{  "iissettings": {    "windowsauthentication":false, //Start Windows Authentication, default to false"anonymousauthentication":true, //Enable anonymous authentication, default to true"iisexpress": {      "ApplicationUrl":"http://localhost:28869",//application-initiated URL path "Sslport":44318//Enable SSL port     }  },  "Profiles": {    "IIS Express": {      "CommandName":"iisexpress",//pass command Arguments "Launchbrowser":true,//whether the browser is starting"Environmentvariables": {//Set environment variable to key-value pair"aspnetcore_environment":"Development"      }    },    "TR.COM.WebSite.Admin": {//select Local self-host boot, see Program.cs file, delete this node will cause the VS boot option missing "CommandName":"Project",      "Launchbrowser":true,      "ApplicationUrl":"https://localhost:5001;http://localhost:5000",//Local Self-host URL"Environmentvariables": {        "aspnetcore_environment":"Development"      }    }  }}

Here, there are two configuration nodes: "IIS Express", "TR.COM.WebSite.Admin", these two nodes, respectively, corresponding to the visual Stuido Start Debugging button drop-down options, you can select the corresponding option to launch the application:

The Launchsettings.json file is used to set the environment in which the application runs in Visual Stuido. We can also add a node, which is automatically added to the drop-down options of the Visual stuido Debug button.

For more information about additional properties, go to this link: http://json.schemastore.org/launchsettings.

1.2. wwwroot

  wwwrootis a folder that holds static content, storing files such as css,js,img.

1.3, appsettings

It is also the name of the application configuration, similar to the Web. config file on the. NET framework, where developers can write system parameters in a appsettings file (such as a connection string for a program) by means of a key-value pair. The startup class also uses the following code in the constructor to enable the program to recognize the file.

Public Startup (IConfiguration configuration) {                   var builder = new Configurationbuilder ()        . Setbasepath (Directory.GetCurrentDirectory ())        . Addjsonfile ("Appsettings.json");        Configuration = Builder. Build ();}

  AppSettings the default settings are as follows:

 {  " logging   " : {  loglevel   "  : {  default  " :  " warning  "  }},   " allowedhosts   ": "  *   " }  

1.4, Startup.cs

The Startup.cs file is an ASP. NET Core boot entry file, you must have tried Owin development will not be unfamiliar. When the project runs, the compiler automatically finds the Startup.cs file read startup configuration in the assembly. In addition to constructors, it can define configure and Configureservices methods. The default Startup.cs settings are as follows

 Public classStartup {
    //Used to start configuration files PublicStartup (iconfiguration configuration) {Configuration=configuration; } PublicIConfiguration Configuration {Get; } //This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {services. Configure<CookiePolicyOptions> (options = { //This lambda determines whether user consent for non-essential cookies are needed for a given request.Options. checkconsentneeded = Context =true; Options. Minimumsamesitepolicy=Samesitemode.none; }); Services. Addmvc (). Setcompatibilityversion (Compatibilityversion.version_2_1); } //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 (); } Else{app. Useexceptionhandler ("/home/error"); App. Usehsts (); } app. Usehttpsredirection (); App. Usestaticfiles (); App. Usecookiepolicy (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}"); }); } }

  Instructions for setting up the Startup.cs:

  (1) constructor: Used to start configuration files

 Public Startup (iconfiguration configuration) {                   varnew  configurationbuilder ()        . Setbasepath (Directory.GetCurrentDirectory ())        . Addjsonfile ("appsettings.json");         = Builder. Build ();}

  

(2) configureservices    configureservices is used to configure the various services in our application, which takes a iservicecollection instance with parameters and optionally returns IServiceProvider. The Configureservices method needs to be called before Configure. For more information, please see the official documentation
 Public void configureservices (iservicecollection services) {       services. Configure<CookiePolicyOptions> (options +            =// this lambda determines Whether user consent for non-essential cookies are needed for a given request.            true ;             = samesitemode.none;        });          Services. Addmvc (). Setcompatibilityversion (compatibilityversion.version_2_1);}
(3) Configure

  The Configure method is used to process various middleware in our program, which determines how our application responds to every HTTP request. 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 configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app.            Usedeveloperexceptionpage (); }            Else{app. Useexceptionhandler ("/home/error"); App.            Usehsts (); } app.            Usehttpsredirection (); App.            Usestaticfiles (); App.            Usecookiepolicy (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}");        }); }

ASP. NET MVC Core Introduction and Project Interpretation

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.