ASP.NET Core introductory tutorial 1. Create project

 

(1) New Project select ASP.NET Core Web Application

(2) automatically installs the appropriate package assembly, at this time there is a dependency exclamation point, exclamation mark disappears waiting for installation

(3) the establishment of other files in the folder of the project, will be displayed in the Project Explorer view

(4) Open the project configuration file and describes: Right on the project under the Resource View - Edit xxx.csproj. Where <TargetFramwork> expressed support for the .net version, where you can edit to support multiple versions. <Folder Include = "wwwroot \"> is a folder, you can see in the resource view, it can be seen in the directory, if you want to see the other directory, you need to set, using whitelist policy. <PacagetReference Include = "Microsoft.AspNetCore.App" /> is a reference packet.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
  </ItemGroup>

</Project>

(5) ASP.Net Core startup and configuration:

Program category

In the application startup, performs CreateWebHostBuilder method, by class in this method Startupcreates a default HostBuilder

Startup Class

    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }

web default project template method calls IApplicationBuilder.run when the project started, the Hello World output in the current HTTP context (HttpContext) in!

(6) dependency injection, IoC container

         The life cycle

         -Transient: in every request creates a new instance

         -Scoped: every Web request will be created once instance

         -Singleton: Once the instance is created, it will have been using this example, until the application is stopped

(7) the benefits of dependency injection:

         - not to manage lifecycle

         - there is no dependency between the type

         For example, the startup class

         public void ConfigureServices(IServiceCollection services)

        {

            // add the code phrase, meaning that if there is another program requests ICinemaService that they will return an instance CinemaMemoryService

            services.AddSingleton<ICinemaService, CinemaMemoryService>();

        }

Lower (8) Startup Configure (IApplicationBuilder app, IHostingEnvironment env): Http request to the pipeline configuration, the special process the user's request, this type of thing is placed is called middleware, such as authorization such as MVC

 

 

IIS is an external server, also known as a reverse proxy server, depending on the different platforms, such as Apache, etc., Kestre application server, cross-platform approach is the implementation of the program Main program. IIS can skip directly access the application server runtime

 

(9) pipeline life cycle: here you need to select the program to run in order to see the console log output, note logger plus here is the new injection parameters

(10) run options to configure: here is the use of VS configuration, configure IIS to run here or direct application runs. The first method, the project right - Properties - Debug - profile; second, direct green run button to select in the interface; the third method, propertie-> launchSettings.json configuration, code env.IsDevelopment () It is used to determine the current operating environment, the implementation process according to the environment.

Guess you like

Origin www.cnblogs.com/xiaoahui/p/11717482.html