Learn Asp.net Core, Free Asp.net Core Tutorial for Beginners, Asp.net Core MVC, Razor Page

Startup.cs in ASP.NET Core

Startup.cs file in asp.net core application contain all configuration and hosting related information.

What is Startup class in Asp.net Core?

Startup class or Startup.cs file is generated as part of the default Asp.net Core template. Startup class is the entry point of application. It configures the request pipeline that handles all requests made to the application.

Startup.cs is used for configuration & Startup Class is mandatory in Asp.net Core application

Can we rename the startup class to some other name?
Yes, Finally this Startup.cs is used in Program class for hosting the application, So renaming this class will not make any difference.

Startup Class Constructor Dependency Injection

Here you can see how new instance of "IConfiguration" is being created, by setting up IConfiguration instance in startup constructor will allow us to access all configuration related information.

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

In Startup file you can see an example of dependency injection

startup.cs in asp.net core

ConfigureServices Method in Startup.cs

In ConfigureService method we can configure any service in that application, to do that all service to be added in IServiceCollection
For example you can see how below methods are added
services.AddMvc(); this was added by default
services.Configure<DbConnection>(Configuration.GetSection("DbConnectionConfig"));

public class Startup
{ // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
 services.Configure<DbConnection>(Configuration.GetSection("DbConnectionConfig"));
    }
}

In above example we are try to configure a DbConnectionConfig class we read information from appSetting.json class, so we want when service starts, all configuration information to be available in custom configuration class

Configure Method in Startup.cs
This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
DbConnectionString = string.Format("Server={0};Database={1};
User ID={2};Password={3};Trusted_Connection=False;
MultipleActiveResultSets=true;",
Configuration["DbConnectionConfig:ServerName"],
Configuration["DbConnectionConfig:DatabaseName"],
Configuration["DbConnectionConfig:UserName"],
Configuration["DbConnectionConfig:Password"]);
app.UseStaticFiles();
app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}
 
Startup.cs in ASP.NET Core
ASP.NET MVC Interview Questions Answers
Hire Asp.net Developer
Asp.Net Core C# Examples | Join Asp.Net MVC Course