Programming/C#

[ASP.NET] App startup in ASP.NET Core

RosyPark 2022. 5. 6. 21:28

Adding Back the Startup Class to ASP.NET Core 6

https://www.youtube.com/watch?v=cuOKV_0b-JI 

 

Project Name: WebAPI 

Program.cs 

using WebAPI;

var app = Startup.InitializeApp(args);

app.Run();

 

 

Startup.cs

namespace WebAPI
{
    public static class Startup
    {
        public static WebApplication InitializeApp(string [] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            ConfigureServices(builder);
            var app = builder.Build();
            Configure(app);
            return app;  
        }

        private static void ConfigureServices(WebApplicationBuilder builder)
        {
            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

        }

        private static void Configure(WebApplication app)
        {
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            app.UseAuthorization();

            app.MapControllers();
        }
    }
}