Startup Class in ASP.NET Core

Startup Class in ASP.NET Core

The startup class is the heart of the ASP.NET Core. It helps to configure the application. By default, it has three modes: Startup constructor, ConfigureService Method, and Configure Method. The following is a default MVC Startup class.

Startup.cs
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    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.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

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

Startup constructor

The first thing in the Startup class is the Startup constructor. By default, it gets the IConfiguration interface. It accepts the following three interfaces.

  • IWebHostEnvironment - It provides a web hosting information
  • IHostEnvironment - Provides information about hosting
  • IConfiguration - It refers to the key/value application configuration properties

ConfigureService Method

Next is a Configure method. This method is called by ASP.NET runtime when the application starts. It gets the IServiceCollection interface. It helps to register services. You can use the service using dependency injection or ApplicantionService.

Configure Method

The final one is the Configure method. This method is called ASP.NET runtime when the application starts. It accepts two parameters, the IApplicationBuilder interface and the IWebHostEnvironment interface. Here you can configure the application request pipeline that runs on each request. In the Startup class code above, you will find the following middleware.

app.UseDeveloperExceptionPage() - Developer Exception Page
app.UseExceptionHandler("/Home/Error") - Exception Handler
app.UseHsts() - HTTP Strict Transport Security
app.UseHttpsRedirection() - HTTPS Redirection
app.UseStaticFiles() - Static Files
app.UseAuthorization() - Authorization
app.UseEndpoints() - Endpoints

These extension methods add the middleware components to the pipeline.

Program.cs

The application starts in the Program.cs file. The Startup class is configured using UseStartup<T>() method in BuildWebHost in 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>();
            });
}
The host provides the services available to the Startup class.

Note: The class for convenience is named Startup. You can change the name of the class.

I hope this helps you. Keep coding.

Comments

Popular posts from this blog

Entity Framework Core (EF) with SQL Server LocalDB

Creating a C# Azure Function with Visual Studio: Step-by-Step Guide

Exploring EventCallback in Blazor: Building Interactive Components