Minimal APIs first appeared in.NET 6 in an effort to provide a more simplified introduction to ASP.NET Core. Prior to this release, there were other conventions to understand in addition to at least three classes to set up: Program.cs, Startup.cs, and an API controller. This process was made simpler with the introduction of Minimal APIs and WebApplicationBuilder.
Advantages of Minimal APIs
- Simplicity and Conciseness
- Ease of Use
- Lightweight Footprint
- Quick Startup Time
- Improved Performance
- Focused on HTTP APIs
- Reduced Configuration Overhead
- Integration with ASP.NET Core
Limitation of Minimal APIs
- Reduced Flexibility: Minimal APIs are designed to be lightweight, which can be limiting in terms of flexibility compared to the full MVC framework. This might be a disadvantage for complex applications with intricate requirements.
- Limited Features: Minimal APIs, as the name suggests, provide a minimalistic set of features. If your application requires advanced features or extensive customization, you might find that the Minimal API approach doesn’t offer all the tools and capabilities you need.
- Learning Curve: While Minimal APIs are intended to simplify development, they may have a learning curve for developers who are more accustomed to the traditional MVC approach. It might take time for developers to adjust to the new conventions and patterns.
- Not Suitable for All Scenarios: Minimal APIs are best suited for lightweight scenarios, microservices, or simple applications. For larger and more complex projects, traditional MVC might be a more appropriate choice.
This is how a basic HelloWorld solution with few APIs looks.
Getting Started
ASP.NET Core Web API should be selected when Visual Studio is open.
Give your project a name and a location.
- Select the framework.
- Check the “Configure for HTTPS” and “Enable OpenAPI support” options.
- Keep “Use controllers” checkbox unchecked.
- Click the “Create” button.
In this section, we will explore the Program.cs file, which is where all the parts of our Minimal API come together. The definition of endpoints and the setup of services are coordinated by this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <span class="token keyword keyword-using">using</span> <span class="token namespace">Microsoft<span class="token punctuation">.</span>AspNetCore<span class="token punctuation">.</span>Http<span class="token punctuation">.</span>HttpResults</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token namespace">MinimalApi</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> builder <span class="token operator">=</span> WebApplication<span class="token punctuation">.</span><span class="token function">CreateBuilder</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Add services to the container.</span> builder<span class="token punctuation">.</span>Services<span class="token punctuation">.</span><span class="token function">AddControllers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> builder<span class="token punctuation">.</span>Services<span class="token punctuation">.</span><span class="token function">AddEndpointsApiExplorer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> builder<span class="token punctuation">.</span>Services<span class="token punctuation">.</span><span class="token function">AddSwaggerGen</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> builder<span class="token punctuation">.</span>Services<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">AddSingleton</span><span class="token generic class-name"><span class="token punctuation"><</span>IBookService<span class="token punctuation">,</span> BookService<span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> app <span class="token operator">=</span> builder<span class="token punctuation">.</span><span class="token function">Build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Configure the HTTP request pipeline.</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>app<span class="token punctuation">.</span>Environment<span class="token punctuation">.</span><span class="token function">IsDevelopment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> app<span class="token punctuation">.</span><span class="token function">UseSwagger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> app<span class="token punctuation">.</span><span class="token function">UseSwaggerUI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> app<span class="token punctuation">.</span><span class="token function">UseHttpsRedirection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Hello Word</span> app<span class="token punctuation">.</span><span class="token function">MapGet</span><span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token string">"Hello, World!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Example 1: Get all books</span> app<span class="token punctuation">.</span><span class="token function">MapGet</span><span class="token punctuation">(</span><span class="token string">"/books"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token class-name">IBookService</span> bookService<span class="token punctuation">)</span> <span class="token operator">=></span> TypedResults<span class="token punctuation">.</span><span class="token function">Ok</span><span class="token punctuation">(</span>bookService<span class="token punctuation">.</span><span class="token function">GetBooks</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">WithName</span><span class="token punctuation">(</span><span class="token string">"GetBooks"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Example 2: Get a specific book by ID</span> app<span class="token punctuation">.</span><span class="token function">MapGet</span><span class="token punctuation">(</span><span class="token string">"/books/{id}"</span><span class="token punctuation">,</span> <span class="token generic-method"><span class="token function">Results</span><span class="token generic class-name"><span class="token punctuation"><</span>Ok<span class="token punctuation"><</span>Book<span class="token punctuation">></span><span class="token punctuation">,</span> NotFound<span class="token punctuation">></span></span></span> <span class="token punctuation">(</span><span class="token class-name">IBookService</span> bookService<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-int">int</span></span> id<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> book <span class="token operator">=</span> bookService<span class="token punctuation">.</span><span class="token function">GetBook</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> book <span class="token keyword keyword-is">is</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token punctuation">?</span> TypedResults<span class="token punctuation">.</span><span class="token function">Ok</span><span class="token punctuation">(</span>book<span class="token punctuation">)</span> <span class="token punctuation">:</span> TypedResults<span class="token punctuation">.</span><span class="token function">NotFound</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">WithName</span><span class="token punctuation">(</span><span class="token string">"GetBookById"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Example 3: Add a new book</span> app<span class="token punctuation">.</span><span class="token function">MapPost</span><span class="token punctuation">(</span><span class="token string">"/books"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token class-name">IBookService</span> bookService<span class="token punctuation">,</span> <span class="token class-name">Book</span> newBook<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> bookService<span class="token punctuation">.</span><span class="token function">AddBook</span><span class="token punctuation">(</span>newBook<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> TypedResults<span class="token punctuation">.</span><span class="token function">Created</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">$"/books/</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">newBook<span class="token punctuation">.</span>Id</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">,</span> newBook<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">WithName</span><span class="token punctuation">(</span><span class="token string">"AddBook"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Example 4: Update an existing book</span> app<span class="token punctuation">.</span><span class="token function">MapPut</span><span class="token punctuation">(</span><span class="token string">"/books/{id}"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token class-name">IBookService</span> bookService<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-int">int</span></span> id<span class="token punctuation">,</span> <span class="token class-name">Book</span> updatedBook<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> bookService<span class="token punctuation">.</span><span class="token function">UpdateBook</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span> updatedBook<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> TypedResults<span class="token punctuation">.</span><span class="token function">Ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">WithName</span><span class="token punctuation">(</span><span class="token string">"UpdateBook"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Example 5: Delete a book by ID</span> app<span class="token punctuation">.</span><span class="token function">MapDelete</span><span class="token punctuation">(</span><span class="token string">"/books/{id}"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token class-name">IBookService</span> bookService<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-int">int</span></span> id<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> bookService<span class="token punctuation">.</span><span class="token function">DeleteBook</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> TypedResults<span class="token punctuation">.</span><span class="token function">NoContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">WithName</span><span class="token punctuation">(</span><span class="token string">"DeleteBook"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
A BookService class for organizing a library of books is provided by the code below, and the IBookService interface establishes the terms of interaction with this service. This is a simple illustration of a service that can manage a library of books using CRUD operations.