- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain the purpose of the Startup class in ASP.NET Core
The Startup class configures your application's services and defines the middleware pipeline.
Generally speaking, the Program class is where you configure your application's infrastructure, such as the HTTP server, integration with IIS, and configuration sources. In contrast, the Startup class defines which components and features your application uses and the middleware pipeline for your app.
Startup.cs
Here is a sample Startup.cs file in a standard ASP.NET Core application.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace TutorialsPoint{ 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?}"); }); } } }
The startup class contains two methods:
ConfigureServices(): Registers the services that your application will need.
Configure(): Configures the middleware pipeline that controls how the application processes the HTTP requests and sends the response.
Services
Services are modular, loosely coupled components that focus on accomplishing one task, such as caching, authentication, etc. In ASP.NET Core, services are simply C# classes that provide specific functionality to an application.
You can use services provided by third-party Nuget libraries, or you can write them yourself. Wherever they are created, they have to be configured in the ConfigureServices() method.
The Startup class uses an IServiceCollection that holds all the services that your application will need. It also configures the dependency injection (DI). So these services are automatically injected by the DI container into your code.
Middleware
Middleware defines how the application will handle the incoming HTTP requests. It also deals with the HTTP response on its way out.
Middleware consists of small modules executing in sequence to transform the incoming request or the outgoing response. The middleware can perform various tasks, including logging, authentication and authorization, service static files, error handling, etc.
One important thing to note is that the order of the middleware is important. The ASP.NET Core framework executes the middleware code in the same order in which you define it.
- Related Articles
- Explain the purpose of the Program class in ASP.NET Core
- What is the use of the Configure() method of startup class in C# Asp.net Core?
- Explain the role of HttpContext class in ASP.NET Core
- What is the purpose of Program.cs file in C# ASP.NET Core project?
- Explain the HTTP request-response lifecycle in ASP.NET Core.
- Explain the purpose of the .csproj file in an ASP.NET application
- Explain how logging works in ASP.NET Core
- Explain how Razor Pages work in ASP.NET Core
- Explain how error handling works in ASP.NET Core
- What is ASP.NET Core? Explain how it's different from the ASP.NET framework.
- What are the benefits of choosing ASP.NET Core over ASP.NET?
- Explain how static files are served in ASP.NET Core
- What is routing? Explain how it works in ASP.NET Core
- Explain how to create a new ASP.NET Core project
- Explain the ASP.NET Core support for multiple environments for development and production
