Service Configuration in Visual Studio 2022 for .Net Core

In visual studio 2022 when you create any asp.net core application, you don’t see any startup.cs file anymore, now all we have to write in program.cs file directly.

If you are working on asp.net core application using earlier version of visual studio, you probably using startup.cs file for any service configuration, now we write almost same code in program.cs

var builder = WebApplication.CreateBuilder(args);

First, let's understand how to add service in service collection list, and then using all those service in application.

var _services = builder.Services;
_services.AddControllersWithViews();
_services.AddSession();

We can keep adding all required services (like logging, authentication, file etc) in service variable _services the same way.

As you can see in above code, we have added session to be used, now we have to tell the application to use the session.

var app = builder.Build();

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

If you want to see how startup file was written in earlier version of visual studio, then check following links.

Asp.Net Core C# Examples | Join Asp.Net MVC Course