asp.netcore 3.1 프로그램, Startup 클래스 상세 정보

14147 단어
Program 클래스
public class Program
    {
        /// 
        ///       
        /// 1.asp.netcore          
        /// 
        /// 
        public static void Main(string[] args)
        {
            //   build  ,          asp.netcore
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup();
                });
    }

Startup 类

public class Startup
    {
        private readonly IConfiguration _configuration;
        /// 
        ///         
        /// 
        /// 
        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
            var aa = _configuration["FyyAspnetcore:Name"];//    appsettings.json     
        }

        // 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
        //                     。    ConfigureServices、    Configure.
            /// 
            ///         
            /// 
            /// 
        public void ConfigureServices(IServiceCollection services)
        {
            /*DI   :
             * 1.  ,     ,Controller         ,
             * 2.      
             * 3.           :Controller               。
             * 4.              :Controller              ,      IOC  DI   。
             */


            //2. IClock     ,IOC       CnClock   。
            services.AddSingleton();//AddSingleton                       ,            ,        ,     。
            //services.AddScoped;//                  。  web        ,web        ,        。
            //services.AddTransient;//               ,          ,         。

            //services.AddControllers();//  webapi   

            //services.AddControllersWithViews();//  mvc   

            //      Json,     。(     ,      )
            services.Configure(_configuration.GetSection("FyyAspnetcore"));
        }

        /// 
        /// Development     ,     。         ,       。
        /// 
        /// 
        /// 
        //public void ConfigureDevelopment(IApplicationBuilder app, IWebHostEnvironment env)
        //{

        //}

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// 
        ///      ,   asp.netcore http     。http       ,           。         ,    ,   ,        。
        ///              ,     (middleware)。
        /// 
        ///      ,             ,         
        ///     
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //      
            //env.IsEnvironment("OK");          
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();//
            }

           // app.UseAuthentication();//       

            app.UseHttpsRedirection();//https       ,       ssl  。

            //      html、js、css      ,      UseStaticFiles    , mvc。
            //          ,              。
            app.UseStaticFiles();

            app.UseRouting();//     。              。

            app.UseEndpoints(endpoints => //    ,       http   url      。           。
            {
                endpoints.MapGet("/", async context =>//  /   url,          。
                {
                    await context.Response.WriteAsync("Hello World!");
                });

                //MVC     ,    ,      
                //endpoints.MapControllerRoute(
                //        name:"default",
                //        pattern:"{controller=Home}/{action=Index}/{id?}"
                //    );

                //      ,   controller action     ,        。
               // endpoints.MapControllers();
            });
        }
    }

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "FyyAspnetcore": {
    "Name": "Fengyinyong",
    "Age": 30
  }
}

좋은 웹페이지 즐겨찾기