ASP.NET Core

Startup Class in ASP.NET Core App

Startup Class in ASP.NET Core App

Startup Class in ASP.NET Core App

The ASP.NET Core 3.1, and ASP.NET Core 5 Applications must include a Startup Class. It is like the Global.asax file our traditional .NET Application
It is executed first when the application starts.
The Startup class can be configured using UseStartup<T>() Extension method at the time of Configuring the Host in the Main() Method of the Program class

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

// webBuilder.UseStartup<Startup>() method
// The name “Startup” is by the ASP.NET Core Convention.

// However, you can give any name to the Startup class, just specify it as the generic parameter in the UseStartup<T>() method.
// For example, to name the Startup class as MyStartup, specify it as .UseStartup<MyStartup>().




ConfigureServices() Method in ASP.NET Core Startup Class
The ConfigureServices method is a place where you can register your dependent classes with the built-in IoC container. After registering the dependent classes, it can be used anywhere in the application. You just need to include it in the parameter of the constructor of a class where you want to use it. The IoC container will inject it automatically.
The ConfigureServices method includes the IServiceCollection parameter to register services to the IoC container

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



Configure() Method in ASP.NET Core Startup Class
The Configure method is a place where we can configure the application request pipeline for our ASP.NET Core application using the IApplicationBuilder instance that is provided by the built-in IoC container.
ASP.NET Core introduced the middleware components to define a request pipeline, which will be executed on every request. You need to include only those middleware components which are required by your application and thus increase the performance of your application.
The default configure method of the ASP.NET Core application with the Empty Project Template includes the following three middleware components

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }  
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}


How Do We Configure the Services and Middleware Components in .NET 6?
in .Net 6, startup file is completly removed, we can do all service & middleware registration in Main method of the Program class





Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram