ASP.NET 6 Minimal Hosting Model Vs. Minimal API

65810666 56 Reputation points
2022-09-26T00:23:34.487+00:00

While reading about the new ASP.NET minimal hosting model, I get confused between two used terms "Minimal Hosting Model" and "Minimal API", which on some blogs they are used interchangeably.
But from my understanding which I want to see if I am correct or not.
The minimal hosting model refer to using the new WebApplication Builder as in the code

    WebApplication.CreateBuilder(args);  
  
Instead of using the underlying IHostBuilder as in the code of ASP.NET 5 that has programs/startup  
  
public static IHostBuilder CreateHostBuilder(string[] args)  
        {  
            return Host.CreateDefaultBuilder(args)  
  
                .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });  
        }  

And minimal host model you can get it by generating the code from the template "webapi" template

And that template and minimal host model doesn't mean you are not using the MVC and Controllers and Controller mapping because the generated code of webapi still has controllers and mapping using

var builder = WebApplication.CreateBuilder(args);  
  
  
builder.Services.AddControllers();  
builder.Services.AddEndpointsApiExplorer();  
  
  
var app = builder.Build();  
  
app.MapControllers();  

But minimal API is different, and it means there are no more controllers and there is no default mapping, and we have to build our own mapping for each URL to actions, as in the code generated by the new .net 6 template "web"
Where the code looks like

var builder = WebApplication.CreateBuilder(args);  
var app = builder.Build();  
  
app.MapGet("/", () => "Hello World!");  
  
app.Run();  

I am correct that they are two different terms, or actually they are the same and minimal API is just the WebApplication builder ?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,611 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,446 Reputation points Microsoft Vendor
    2022-09-26T06:02:23.283+00:00

    Hi @65810666 ,

    ASP.NET 6 Minimal Hosting Model Vs. Minimal API

    They are different technologies.

    The Asp.Net 6 minimal hosting model:

    • Significantly reduces the number of files and lines of code required to create an app. Only one file is needed with four lines of code.
    • Unifies Startup.cs and Program.cs into a single Program.cs file.
    • Uses top-level statements to minimize the code required for an app.
    • Uses global using directives to eliminate or minimize the number of using statement lines required.

    For example, when using Asp.Net 5 (or previous version) Web App template to generate the application, the project contains the Startup.cs file and Program.cs file. But in Asp.net 6, it will use the minimal hosting model, the preceding code is replaced by the following:

    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();  
    

    More detail information, see New hosting model.

    For the Minimal API, as the name suggests, it is a minimal version of the API application.

    Generally, when we create a Asp.net Core API application, instead of the Minimal API application, it will create a Controller folder, and we can use the app.MapControllers(); to configure the route. When you want to add a new end point, you can add a new API controller in the Controller folder, and then add the relates action method.

    244655-image.png

    But when we create a Minimal API application, there doesn't have the controller folder. The endpoints are all in the Program.cs file (use app.MapGet, app.MapPost and so on):

    244589-image.png

    More detail information, see Tutorial: Create a minimal web API with ASP.NET Core

    Differences between minimal APIs and APIs with controllers

    So, the Minimal Hosting Model and Minimal API are two different features in the Asp.Net 6.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.