Loading

ASP.NET Core

How to setup ASP.NET Core Startup Class?. The Complete ASP.NET Core Developer Course 2022 [Videos].

The Startup class configures your applications services and defines the middleware pipeline. Generally speaking, the Program class is where you configure your applications infrastructure, such as the HTTP server, integration with IIS, and configuration sources.

ASP.NET Core Startup Class

In this Video, I am going to discuss the ASP.NET Core Startup class in detail. Please watch our previous Video where we discussed the ASP.NET Core launchSettings.json file.

Startup Class in ASP.NET Core Application:

The ASP.NET Core application 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 Program class. Please have a look at the below Program class and please focus on the webBuilder.UseStartup<Startup>() method.

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 Startup class in Visual Studio by clicking on the Startup.cs class file in the solution explorer. The following is a 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 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 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 class, 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 watch “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 parameter this method accepts as shown in the below image.

ConfigureServices() method in ASP.NET Core Startup class

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 include only those middleware components which are required by your application and thus increase the performance of your application.

The default configure method of ASP.NET Core application with the Empty template includes the following three middlewares as shown in the below image.

Configure() method in ASP.NET Core Startup class

As we progress in this course, we will discuss more these two methods.

See All

Comments (728 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core / Blog

What is ASP.NET Core?

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform.
27-jan-2022 /16 /728

ASP.NET Core / Blog

What is ASP.NET Core Framework?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /728

ASP.NET Core / Blog

How to Setup ASP.NET Core Environment ?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /728