{"id":28,"date":"2024-08-24T11:32:42","date_gmt":"2024-08-24T10:32:42","guid":{"rendered":"https:\/\/contentlabstudy.com\/soft\/?p=28"},"modified":"2026-09-24T21:15:33","modified_gmt":"2026-09-24T20:15:33","slug":"rest-endpoints-with-net-core-minimal-api","status":"publish","type":"post","link":"https:\/\/contentlabstudy.com\/soft\/rest-endpoints-with-net-core-minimal-api\/","title":{"rendered":"REST endpoints with ASP.NET Core Minimal APIs"},"content":{"rendered":"<p><strong>Minimal APIs<\/strong> in ASP.NET Core map HTTP routes to handlers without a controller class. They are still ASP.NET Core (the product is <strong>.NET<\/strong>, not \u201c.NET Core\u201d except as history). Use them for small HTTP surfaces and vertical slices. Controllers remain fine for large apps with filters and conventions you already have.<\/p>\n<h2>A slice, not a dump of Program.cs<\/h2>\n<pre><code>var builder = WebApplication.CreateBuilder(args);\nbuilder.Services.AddScoped&lt;IOrders, SqlOrders&gt;();\nvar app = builder.Build();\napp.MapGet(\"\/orders\/{id:guid}\", async (Guid id, IOrders orders, CancellationToken ct) =&gt;\n{\n    var order = await orders.Get(id, ct);\n    return order is null ? Results.NotFound() : Results.Ok(order);\n});\napp.Run();<\/code><\/pre>\n<ul>\n<li>Bind route\/query\/body to parameters; inject services the same way as MVC.<\/li>\n<li>Return <code>IResult<\/code> (<code>Results.Ok<\/code>, <code>TypedResults<\/code>) so status codes stay explicit.<\/li>\n<li>Group with <code>MapGroup(\"\/orders\")<\/code> and attach auth\/filters once.<\/li>\n<\/ul>\n<h2>When to prefer controllers<\/h2>\n<p>Dozens of similar resources, shared filters, API versioning attributes, or a team that already thinks in controller actions. Minimal APIs do not magically make a 200-endpoint monolith simpler; folder-by-feature does.<\/p>\n<h2>HTTP hygiene<\/h2>\n<ul>\n<li>GET\/HEAD have no side effects; do not \u201cGET to delete.\u201d<\/li>\n<li>Use 401\/403\/404\/409\/422 honestly. 200 + <code>{ success: false }<\/code> is a lie.<\/li>\n<li>Problem details (<code>application\/problem+json<\/code>) beat ad-hoc error JSON.<\/li>\n<\/ul>\n<h2>What breaks it<\/h2>\n<p>GET cancels an order, and a 200 body says <code>success: false<\/code>. Cancel with POST. Return 404 when the order is missing and 409 when it is already cancelled.<\/p>\n<pre><code>app.MapGet(\"\/orders\/{id:guid}\/cancel\", async (Guid id, IOrders orders, CancellationToken ct) =&gt;\n{\n    await orders.Cancel(id, ct);\n    return Results.Ok(new { success = false });\n});<\/code><\/pre>\n<pre><code>app.MapPost(\"\/orders\/{id:guid}\/cancel\", async (Guid id, IOrders orders, CancellationToken ct) =&gt;\n{\n    var result = await orders.Cancel(id, ct);\n    return result switch\n    {\n        CancelResult.Missing =&gt; Results.NotFound(),\n        CancelResult.Conflict =&gt; Results.Conflict(),\n        _ =&gt; Results.NoContent()\n    };\n});<\/code><\/pre>\n<h2>Pitfalls<\/h2>\n<ul>\n<li>One 3,000-line <code>Program.cs<\/code>.<\/li>\n<li>Capturing scoped DbContext in a singleton lambda.<\/li>\n<li>Calling it REST because the path has a noun. See <a href=\"\/soft\/architectural-styles\/\">architectural styles<\/a>.<\/li>\n<\/ul>\n<p>Auth: <a href=\"\/soft\/rest-minimal-api-with-authentication\/\">REST Minimal API with Authentication<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Minimal APIs in ASP.NET Core map HTTP routes to handlers without a controller class. They are still ASP.NET Core (the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":4,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":3226,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/28\/revisions\/3226"}],"wp:attachment":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}