When NET 6
was released, the first big change was the lack of Startup.cs
. This is very nice and all. But sometimes we just want it back, and here is how.
First, create a new file called Startup.cs
at the root of your project with the following content - don't forget the namespace.
public class Startup
{
readonly IConfiguration configuration;
public Startup(IConfiguration configuration)
{
this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
}
}
Then at Program.cs change to the following.
//add necessary usings
var builder = WebApplication.CreateBuilder(args);
//create new instance of Startup
var startup = new Startup(builder.Configuration);
//configure all services
startup.ConfigureServices(builder.Services);
var app = builder.Build();
//configure the pipeline
startup.Configure(app, builder.Environment);
//run
app.Run();
The simplest solution is the better!
That is it. Enjoy.
Top comments (7)
The
Program.cs
seems to be missed by many developers!I happened to look at the preview of .NET 7 and you should be pretty happy since it introduce back the
Program.cs
for those who want it. All you will need to do is appending--use-program-main
to your command line:This will also be supported directly by Visual Studio (I just find odd that a checkbox is for not enabling something):
Hope it might save @stphnwlsh the extension method and give an alternative to @kaylumah!
Super cool
Hey that's pretty cool, but honestly I don't mind the new setup. I just don't want everything in the one file. It's maybe cool for small projects or little things but when you're configuring logging, telemetry, swagger etc that
Program.cs
blows out quickly.I can easily create extension methods on the
WebApplicationBuilder
andWebApplication
in say aProgramExtensions.cs
file and I find it helps with that separation, just likeStartup.cs
did.Also not a huge fan of the minimal api setup, but if when implementing those, I'd use extension methods to separate out pseudo controllers too
I have to admit that you are right, the
Program.cs
does look like a good idea but the fact that it is blowing up when configuring a bigger application might show that it may not be the best way to reduce the boilerplateAs for minimal API it seems that a lot of people are doing the same. Still in .NET 7, they introduces
RouteGroups
to create such pseudo-controllers and keep it a little bit more coherentHowever, I think that it may not be ideal if you are defining endpoints in a single pseudo-controller and they all have specific configuration as for their routes / authorization / metadata / etc.
I think people will still use the Startup and Program classes for a while because most of the books on ASP.NET still have them as examples.
Thanks for making this reference available!
I am still not sure what I think of the change they made to the template. Sure it's nice to write less code, and I supose its more beginner friendly. But in my opinion its also less verbose.
I've just build into the new format with extension methods. Have to fix the naming but works for me.