Back to: ASP.NET Core Tutorials For Beginners and Professionals
ASP.NET Core Startup Class
In this article, I am going to discuss the ASP.NET Core Startup Class in detail with Examples. Please read our previous article where we discussed the ASP.NET Core launchSettings.json file with Examples. Before proceeding with this article, let me tell you one important point that, this Startup.cs class file is deprecated from .NET 6. So, if you are working on .NET 6 or any higher version .NET project, then you will not find this Startup.cs class file. From .NET 6, this Startup class code is merged with the Main method of the Program class. So, in this article, First I will explain the Startup class using ASP.NET Core 3.1 Project and then I will explain how you can achieve the same functionality using the Main method of the Program class with .NET 6 Project.
Startup Class in ASP.NET Core Application:
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. As the name suggests, 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. Please have a look at the below Program class and please focus on the webBuilder.UseStartup<Startup>() method. You can only find the code if you are developing the application using ASP.NET Core 3.1.
namespace FirstCoreWebApplication { 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 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>().
Open the Startup Class in Visual Studio by clicking on the Startup.cs class file in the solution explorer. The following is the default Startup class in ASP.NET Core 3.x.
namespace FirstCoreWebApplication { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // 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(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); }); } } }
As you can see in the above code, the Startup class includes two public methods: ConfigureServices and Configure. The Startup class must include the Configure method and can optionally include the ConfigureService method.
ConfigureServices() Method in ASP.NET Core Startup Class
The Dependency Injection Design Pattern is used heavily in ASP.NET Core Architecture. It includes the built-in IoC container to provide dependent objects using constructors.
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.
ASP.NET Core Refers to the dependent class as a Service. So, whenever you read “Service” then understand it as a class that is going to be used in some other class.
The ConfigureServices method includes the IServiceCollection parameter to register services to the IoC container. For example, if you want to add RazorPages services or MVC services to your ASP.NET Core Application, then you need to add those services to the IServiceCollection parameter this method accepts as shown in the below image.
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 as shown in the below image.
As we progress in this course, we will discuss more about these two methods.
How Do We Configure the Services and Middleware Components in .NET 6?
Now, you might have one question on your mind if we don’t have the Startup class in .NET 6 Applications, then how do we configure the services and middleware (Request Processing Pipeline) components? The answer is the Main method of the Program class. Now, within the Main method, we can configure the services as well as we can configure the middleware components. For a better understanding, please have a look at the Main method of the Program class as shown below.
using System.Text.RegularExpressions; namespace FirstCoreWebApplication { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); //Adding Services to the IOC Container // Add MVC Service builder.Services.AddControllersWithViews(); // Add Logging Service builder.Services.AddLogging(); //Adding Middleware Components or Configuring HTTP Request Pipeline. if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Worker Process Name : " + System.Diagnostics.Process.GetCurrentProcess().ProcessName); }); }); app.Run(); } } }
As you can see in the above code, we have added two services i.e. MVC and Logging as well as we have added a few Middleware components for the Application Request Processing Pipeline such as UseDeveloperExceptionPage, UseRouting, UseAuthorization, UseEndpoints, etc. As we progress in this course, we will discuss what all these Services and Middleware Components are and what is their use in ASP.NET Core Applications.
In the next article, I am going to discuss the ASP.NET Core Appsettings.json File in Detail with Examples. Here, in this article, I try to explain the ASP.NET Core Startup Class in Detail with Examples. I hope this article will help you to understand the need and use of ASP.NET Core Startup Class with Examples.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.