net-core
  1. net-core-startupcs-file-overview

Startup.cs file overview - ( ASP.NET Core Web Apps )

The Startup.cs file is an essential part of the ASP.NET Core web application. It contains all the essential configurations to set up the application pipeline, services, middleware, and more.

Syntax

The Startup.cs file is a C# class file and follows the standard object-oriented syntax for defining classes and methods. It includes two primary methods to set up the middleware pipeline: ConfigureServices() and Configure().

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
      // Add services to the application dependency injection container.
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      // Configure the HTTP request pipeline.
    }
}

Example

Here's an example of a typical Startup.cs file in an ASP.NET Core Web application:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;

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

    public IConfiguration Configuration { get; }

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

        services.AddDbContext<MyDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection")));
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseMigrationsEndPoint();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

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

Explanation

The ConfigureServices method registers services that the application depends on. It uses the dependency injection container to register services that are used throughout the app. For example, the app's database context can be registered here.

The Configure method sets up middlewares like exception handling, static files, and routing. At the end of the method, it maps HTTP requests to the appropriate endpoint.

Use

The Startup.cs file is responsible for setting up and configuring an ASP.NET Core web application. You can include libraries, services, and middle-wares in your application that are essential to building a robust web application.

Important Points

  • The Startup.cs file is located in the Root directory of the project.
  • The file contains two primary methods, Configure and ConfigureServices. The Configure method sets up the middleware pipeline, while ConfigureServices focuses on dependency injection.
  • The methods in Startup are executed in sequence when the application is run.

Summary

The Startup.cs file is essential in ASP.NET Core web applications. It allows developers to control the dependencies, services, and middleware that an application uses. In this page, we discussed the syntax, example, explanation, use, important points, and summary of the Startup.cs file.

Published on: