What is the Startup class and Program.cs in ASP.NET Core

In anticipation of the start of the "C # ASP.NET Core Developer" course, we have prepared a traditional translation of useful material.



We also invite you to an open webinar on the topic
"Differences in structural design patterns by example". At the webinar, participants, together with an expert, will consider three structural design patterns: Proxy, Adapter and Decorator; and also write some simple programs and refactor them.






Introduction

Program.cs β€” , . Program.cs ASP.NET Core , Program.cs .NET Framework. Program.cs Startup.cs, IISIntegration IWebHostBuilder, Main



.





Global.asax ASP.NET Core. ASP.NET Core Global.asax Startup.cs.





Startup.cs β€” , Program.cs . . Startup .





Program.cs?





Program.cs β€” , . Program.cs IWebHost, -.





public class Program {  
    public static void Main(string[] args) {  
        BuildWebHost(args).Run();  
    }  
    public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args).UseStartup < startup > ().Build();  
}  
      
      



WebHost IWebHost, IWebHostBuilder IWebHostBuilder, . CreateDefaultBuilder()



WebHostBuilder.





UseStartup()



Startup



, -. Startup.





Build()



IWebHost, Run()



- .





 Program.cs ASP.NET Core -.





public static IWebHostBuilder CreateDefaultBuilder(string[] args) {  
    var builder = new WebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).ConfigureAppConfiguration((hostingContext, config) => {  
        /* setup config */ }).ConfigureLogging((hostingContext, logging) => {  
        /* setup logging */ }).UseIISIntegration()  
    return builder;  
}
      
      



UseKestrel()



, Kestrel -. Kestrel β€” - ASP.NET Core . Asp.Net Core, IIS (UseIISIntegration()



), .





UseIISIntegration()



, UseContentRoot()



, UseEnvironment(Development)



, UseStartup()



, Appsetting.json Environment Variable. UseContentRoot



.





loglevel, .  loglevel, appsetting.json.





.ConfigureLogging(logging => 
{ logging.SetMinimumLevel(LogLevel.Warning); }) 
      
      



, program.cs, .





.ConfigureKestrel((context, options) => 
{ options.Limits.MaxRequestBodySize = 20000000; });  
      
      



ASP.net Core , ( IIS, -), IIS, Apache, Nginx . .





Startup?

startup.cs ? , startup.cs , , , public, private, internal. Startup. ASP.NET Core .





Startup{EnvironmentName}



, EnvironmentName



Startup (Environment Specific), // .





ASP.NET Core , Development, Production Staging. ASPNETCORE_ENVIRONMENT



(into Hosting Environment interface).





startup.cs



? , Startup.





Startup , ConfigureServices Configure, .





Startup





public class Startup {  
    // Use this method to add services to the container.  
    public void ConfigureServices(IServiceCollection services) {  
        ...  
    }  
    // Use this method to configure the HTTP request pipeline.  
    public void Configure(IApplicationBuilder app) {  
        ...  
    }  
}  
      
      



ConfigureServices

Startup, . - , ConfigureService



.





ConfigureServices



IServiceCollection



. public, .





public void ConfigureServices(IServiceCollection services)  
{  
   services.AddMvc();  
}  
      
      



Configure

Configure



, HTTP-. (middleware) HTTP-. IApplicationBuilder



, IHostingEnvironment



ILoggerFactory



. - ConfigureService



, Configure



.





public void Configure(IApplicationBuilder app)  
{  
   app.UseMvc();  
}
      
      



, MVC . UseMvc()



Configure AddMvc()



ConfigureServices



. UseMvc



. (Middleware) β€” , Asp.net Core. , .





app.UseHttpsRedirection();  
app.UseStaticFiles();  
app.UseRouting();  
app.UseAuthorization();  
UseCookiePolicy();  
UseSession(); 
      
      



http- Use



, Run



Map



.





Run

Run HTTP-. , , , HTTP-.





Use

( next) , HTTP- , .





Map

Map , .





app.Map("/MyDelegate", MyDelegate);
      
      



,





ASP.net Core (Dependency Injection



). , . β€” Startup



.





AddTransient

Transient () ; .





Scoped

Scoped β€” , .





Singleton

Singleton β€” .





startup.cs Program.cs?





: , .





, program.cs startup.cs Asp.net Core . (Environment Variable), , . , , UseIIsintegration()



UseKestrel()



.






Β«C# ASP.NET Core Β»





Β« Β».








All Articles