Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now

How to Add Startup.cs in ASP.NET Core 6 Project

 

Here we are going to learn how to add the startup.cs file in the Asp.net core application when we are working with the ASP.NET 6.0 project, so let's start.

If you are creating your asp.net core application with 6.0 then you will not see the Startup.cs class in project files, as we used to see in the previous version of the asp.net core application, and we used to register the dependencies of the application and the middleware.

So in ASP.NET 6.0, Startup.cs class is removed and Program.cs class is the place where register the dependencies of the application and the middleware.

But if you still want to write your middleware and dependencies in startup class, here we will learn how to add the startup.cs class in ASP.NET 6.0 project.

Below is the Program.cs file which exists in ASP.NET 6.0 project

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) {
    app.UseExceptionHandler("/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.MapRazorPages();
app.Run();
C#

As you can see above code, middleware and services are now written in Program.cs class. As we know Startup.cs contains 2 methods, ConfigureServices() and Configure() and we register the dependency and services in ConfigureServices() and Middlewares in Configure() method.

Now in asp.net 6.0, Program.cs, is the place where you need to register your services and dependencies after the builder.Services.AddRazorPages(); and middleware after var app = builder.Build();.

Note
Order matters while registering your middleware in the pipeline.

Now To add Startup.cs to ASP.NET Core 6.0 project, add a new class named Startup.cs and add the below code.

public class Startup {
    public IConfiguration configRoot {
        get;
    }
    public Startup(IConfiguration configuration) {
        configRoot = configuration;
    }
    public void ConfigureServices(IServiceCollection services) {
        services.AddRazorPages();
    }
    public void Configure(WebApplication app, IWebHostEnvironment env) {
        if (!app.Environment.IsDevelopment()) {
            app.UseExceptionHandler("/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.MapRazorPages();
        app.Run();
    }
}
C#

So in the above code, you can see, that we have added a startup class and added 2 methods ConfigureServices and Configure as we used to have in the previous version of asp.net.

After adding the above file now remove Dependencies and services code from the program.cs class and put that code into ConfigureServices() method and same middleware code in Configure method and remove from the program.cs class.

After updating our Program.cs and Startup.cs class, we have to call startup.cs class from program.cs class like below

var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services); // calling ConfigureServices method
var app = builder.Build();
startup.Configure(app, builder.Environment); // calling Configure method
C#

Now if you run your application, it should run without any error. 

In the above code, we can see, that we are calling startup.cs classes methods (ConfigureServices, Configure) from Program.cs class. So now we are not bounded to write method name as ConfigureServices and Configure method, As we used to do in the previous version of asp.net.

Previously it was not possible to change the startup.cs methods name, but now we can write method name as we want and write the respective code in these methods and can call same from the program.cs class.


--------------------------------------------------------------------------------------------------------------------

Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now