Ed Andersen

Software Developer and Architect in Japan

Converting .NET 6 Minimal API back to Startup.cs

Ed Andersen Avatar

by

in ,

I’ve had to convert a Minimal API app back to Startup.cs a few times now. Here are some reasons why you might want to do it:

Incompatible NuGet packages

For example as of time of writing Amazon.Lambda.AspNetCoreServer doesn’t work with Minimal APIs. If you didn’t know that Amazon.Lambda.AspNetCoreServer.Hosting was what you needed for Minimal APIs, you’d be stuck without converting to Startup.cs.

Needing to return to .NET 5

Again, as of time of writing, AWS Elastic Beanstalk does not support .NET 6 on runtime based deployments. So if you have built half your app in Minimal API style you’ll need to change it to Startup.cs to use .NET 5.

It is different to everything else in your org

Minimal APIs introduce new training overhead for teams used to Startup.cs. Remember, some people are only just getting their heads around Startup.cs which was a big (and brilliant) change from Global.asax.

It doesn’t match documentation

Most documentation out there still has instructions for Startup.cs. Figuring out how to apply the instructions in a Minimal API Program.cs can be a headache.

If you want to convert to a Startup class, here are the empty Program.cs and Startup.cs classes that will be useful

Program.cs:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

public class Program
{
    public static void Main(string[] args)
    {
        var host = CreateWebHostBuilder(args).Build();
        host.Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
}

and Startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

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

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {

    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

    }
}

Here is a video walking through how to do it:

Ed Andersen Avatar

About me

Hi! 👋 I’m a Software Developer, Architect and Consultant living in Japan, building web and cloud apps. Here I write about software development and other things I find interesting. Read more about my background.

Ed’s “Newsletter”

Get the latest blog posts via email ✌️


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *