ASP.NET Core project configuration (Startup) - ASP.NET Core Basics Tutorial - simple tutorial, simple programming

Original: ASP.NET Core project configuration (Startup) - ASP.NET Core Basics Tutorial - simple tutorial, simple programming

 

ASP.NET Core project configuration (Startup)

The previous chapters we have introduced and used  Startup categories, including

  1. Modified when creating a new project  Startup class  Hello World modify the return value
  2. In the previous chapter to explain  Program.cs talked about when  Program the class is instantiated  Startup class

But  Startup can do more than that, it can be said ASP.NET Core in large and small are various components and middleware and  Startup class deal.

But all can not finish a chapter. Ever since, this chapter we will explain a few things most important

previous version

If you've ever used ASP.NET, then you might expect

  1. See a global.asax file, a place you can write code to perform during start-up Web applications

  2. See a web.config file that contains all the configuration parameters for the application to be executed

In ASP.NET Core, these files all but disappeared, replaced by the use of Startup.cs load configuration and startup code

Startup.cs file has a Startup class, in this class you can configure the application, even Configuring source

The default file content Startup.cs

Startup.cs file in the default content below

the using the System ;
 the using the System.Collections.Generic ; the using the System.Linq ; the using System.Threading.Tasks ; the using Microsoft.AspNetCore.Builder ; the using Microsoft.AspNetCore.Hosting ; the using Microsoft.AspNetCore.Http ; the using Microsoft.Extensions.DependencyInjection ; namespace the HelloWorld { public class the Startup { // this method is called at run time. // You can use this method to add the service to the container // For more information about configuring the application, you can view https://go.microsoft.com/fwlink/?LinkID=398940public void ConfigureServices ( IServiceCollection Services ) { } // This method is invoked at run time // This method can be used to configure the HTTP request pipeline public void the Configure ( IApplicationBuilder App , IHostingEnvironment the env ) { IF ( the env . IsDevelopment ()) { App . UseDeveloperExceptionPage (); } App . the Run ( the async ( context ) => { the awaitcontext.Response.WriteAsync("Hello World!"); }); } } } 

Startup Class

Startup Class can be used to define the configuration request processing pipeline and application services required.

Startup Class must be public, and must include the following two methods

  1. ConfigureServices() 方法

    public void ConfigureServices(IServiceCollection services){}
    

    ConfigureServices() The method used to define the services needed by the application, such as ASP.NET Core MVC, Entity Framework Core Identity and etc.

    About  the service  we will further detail in the following sections

  2. Configure () method

    public void Configure(IApplicationBuilder app, IHostingEnvironment env){}
    

    Configure() It is used to define the request pipeline middleware

    On  request pipeline  concept, we will detail in the next section

Project Configuration

Startup Class  Configure() where a method for configuring and constructing the HTTP request pipeline

How to understand the Bible?

In fact, that is to say, Configure() the method can be used to define our application how to respond to the request

For any web site, by default it will only output Hello World!

If we want different behavior of the application, we need  Configure() to add additional code to change the method in pipeline

For example, if we want to, such as  index.html serving static files and the like, you need to  Configure() add some code method

For example, if you want to send an error page or routing requests to the ASP.NET MVC controller, we need this  Configure() to do some work methods

Dynamic response content

By default, we have provided in response to a hard-coded for each request Hello World!

Took over to us no longer use hard-coded, but load a string from a component to respond to each request

  1. In Solution Manager  HelloWorld  right-click on the item and select  Add -> New File

    If your computer is Windows, it is the  Add -> New Item

  2. In the New File dialog box, select the left of the  Web , and then select the right of the  empty JSON file

    If your computer is Windows, it is first checked  ASP.NET Core  under  conventional  (Genreral) and then select  JSON file

  3. Enter the name  AppSettings.json , then click the lower-right corner of  the New  button to add a  AppSettings.json  file

    If your computer is Windows, then clicking on  Add  button

  4. Then double-click  AppSettings.json  open the file, enter the following

    {
        "Message": "Hello World \ n Hello, simple tutorial, your website is www.twle.cn it!?"
    }
    
  5. Double-click to open the  Startup.cs file,  Startup add a read-write property class Configuration

    public IConfiguration Configuration { get; set; }
    
  6. Modified  Startup class, adding  Startup() a constructor, loading  AppSettings.json  file and then builds CI

    public Startup() 
    { 
        var builder = new ConfigurationBuilder().AddJsonFile("AppSettings.json"); 
        Configuration = builder.Build(); 
    }
    
  7. Last modified  Configure() method, read from the configuration item  message and a content of the response

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
    
        app.Run(async (context) =>
        {
            var msg = Configuration["message"];
            await context.Response.WriteAsync(msg);
        });
    }
    

The modified code is completed as follows

Startup.cs

using System;
using System.IO; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace HelloWorld { public class Startup { public Startup() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("AppSettings.json"); Configuration = builder.Build(); } public IConfiguration Configuration { get; set; } // 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) => { var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); } } } 

Click the green triangle Run the project (if the project has been directly refresh the browser can be run)

Refresh your browser to display the following results

Guess you like

Origin www.cnblogs.com/jiejiehencool/p/11096617.html