Using Startup class in .Net 6

One of the most important changes that we saw in the last version of .net templates for .NET 6 is that we don’t have a Startup Class anymore, everything that you’ll see is just the class Program.

The project will look like this (ASP.Net Web Applications MVC).

And the Program class will have this code.

it looks familiar to the templates that we have for minimal APIs, but in this case, we are creating an MVC web application, now we have all the configurations in our class called Program, the service instance container, filters, middleware, and the same things that we used to have in the methods ConfigureService and Configure in the class called Startup.

we would like to have those things separate for a better project organization, for most of us.

Now I’ll show you how to add the class Program to your .NET 6 project template.

The first thing we shall do is, add a new class to our project at the root level the same as Program and we shall call this class Startup and add the following code to the class.

using Microsoft.AspNetCore.Builder;

namespace DotNetSixStartup
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

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

        public void ConfigureServices(IServiceCollection services) 
        { 
        
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            
        }
    }
}

After that, we are removing some code or copying to add that code to the Startup class, I will show you which code you will remove or copy, look at the following image.

The first box in the image is referring to the new way to add Services to our project, you can copy the code and remove from the program class and paste it into the ConfigureServices method. But one observation, you will have to remove “builder.” And use the “Services” word in lower case to refer to the “services” property that we are receiving in the method.

For the second box, you can still move it as you already had here and paste it into the Configure method, you will only have to do minor changes and I will show you which one.

  1. In the condition for getting the Environment, we shall replace “app.Environment” with “env.”
  2. In the last line of code, we are still getting an error because we are using MapControllerRoute directly, and in this case, we have to replace this code we this one:

The last thing you will have to do is just added those lines of code to your Program class (you can copy and replace the entire code of your class Program with this one).

using DotNetSixStartup;

var builder = WebApplication.CreateBuilder(args);

var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);

var app = builder.Build();

startup.Configure(app,app.Environment);

app.Run();

and our two classes must look like that.

Program.cs

Startup.cs

Now we can run our project and see everything is working as expected, now you can start working on your project using and Startup class.

Leave a Reply

Your email address will not be published.