{"id":28,"date":"2024-08-24T11:32:42","date_gmt":"2024-08-24T10:32:42","guid":{"rendered":"https:\/\/contentlabstudy.com\/soft\/?p=28"},"modified":"2024-08-24T11:32:57","modified_gmt":"2024-08-24T10:32:57","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 .NET Core Minimal Api"},"content":{"rendered":"\n<p>Creating REST endpoints using .NET Core Minimal API is a streamlined approach to building web APIs with less boilerplate code. Below is an example of how to create several REST endpoints using .NET Core Minimal API. This example assumes you are working with a simple model, such as a <code>Product<\/code>, and provides endpoints for basic CRUD operations (Create, Read, Update, Delete).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Set Up the Project<\/h3>\n\n\n\n<p>First, create a new .NET Core Minimal API project:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopy code<code>dotnet new web -n MinimalApiExample\ncd MinimalApiExample\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Define the Model<\/h3>\n\n\n\n<p>Create a simple <code>Product<\/code> class that will represent the data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>public class Product\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n    public decimal Price { get; set; }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Set Up the In-Memory Data Store<\/h3>\n\n\n\n<p>For simplicity, we&#8217;ll use an in-memory list to store the products:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>var products = new List&lt;Product&gt;\n{\n    new Product { Id = 1, Name = \"Product 1\", Price = 10.00m },\n    new Product { Id = 2, Name = \"Product 2\", Price = 20.00m },\n    new Product { Id = 3, Name = \"Product 3\", Price = 30.00m }\n};\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create the Minimal API Endpoints<\/h3>\n\n\n\n<p>In the <code>Program.cs<\/code> file, define your Minimal API endpoints:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>var builder = WebApplication.CreateBuilder(args);\nvar app = builder.Build();\n\nvar products = new List&lt;Product&gt;\n{\n    new Product { Id = 1, Name = \"Product 1\", Price = 10.00m },\n    new Product { Id = 2, Name = \"Product 2\", Price = 20.00m },\n    new Product { Id = 3, Name = \"Product 3\", Price = 30.00m }\n};\n\n\/\/ Get all products\napp.MapGet(\"\/products\", () =&gt;\n{\n    return Results.Ok(products);\n});\n\n\/\/ Get a product by ID\napp.MapGet(\"\/products\/{id:int}\", (int id) =&gt;\n{\n    var product = products.FirstOrDefault(p =&gt; p.Id == id);\n    if (product is null)\n    {\n        return Results.NotFound();\n    }\n    return Results.Ok(product);\n});\n\n\/\/ Create a new product\napp.MapPost(\"\/products\", (Product product) =&gt;\n{\n    product.Id = products.Max(p =&gt; p.Id) + 1;\n    products.Add(product);\n    return Results.Created($\"\/products\/{product.Id}\", product);\n});\n\n\/\/ Update an existing product\napp.MapPut(\"\/products\/{id:int}\", (int id, Product updatedProduct) =&gt;\n{\n    var product = products.FirstOrDefault(p =&gt; p.Id == id);\n    if (product is null)\n    {\n        return Results.NotFound();\n    }\n\n    product.Name = updatedProduct.Name;\n    product.Price = updatedProduct.Price;\n\n    return Results.Ok(product);\n});\n\n\/\/ Delete a product\napp.MapDelete(\"\/products\/{id:int}\", (int id) =&gt;\n{\n    var product = products.FirstOrDefault(p =&gt; p.Id == id);\n    if (product is null)\n    {\n        return Results.NotFound();\n    }\n\n    products.Remove(product);\n    return Results.NoContent();\n});\n\napp.Run();\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Run the Application<\/h3>\n\n\n\n<p>To run the application, use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopy code<code>dotnet run\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Test the Endpoints<\/h3>\n\n\n\n<p>You can test the endpoints using tools like <code>curl<\/code>, Postman, or directly in a browser.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Get all products<\/strong>:httpCopy code<code>GET \/products<\/code><\/li>\n\n\n\n<li><strong>Get a product by ID<\/strong>:httpCopy code<code>GET \/products\/1<\/code><\/li>\n\n\n\n<li><strong>Create a new product<\/strong>:httpCopy code<code>POST \/products Content-Type: application\/json { \"name\": \"New Product\", \"price\": 15.50 }<\/code><\/li>\n\n\n\n<li><strong>Update an existing product<\/strong>:httpCopy code<code>PUT \/products\/1 Content-Type: application\/json { \"name\": \"Updated Product\", \"price\": 12.00 }<\/code><\/li>\n\n\n\n<li><strong>Delete a product<\/strong>:httpCopy code<code>DELETE \/products\/1<\/code><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Summary of REST Endpoints:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GET <code>\/products<\/code><\/strong>: Retrieve all products.<\/li>\n\n\n\n<li><strong>GET <code>\/products\/{id}<\/code><\/strong>: Retrieve a specific product by ID.<\/li>\n\n\n\n<li><strong>POST <code>\/products<\/code><\/strong>: Create a new product.<\/li>\n\n\n\n<li><strong>PUT <code>\/products\/{id}<\/code><\/strong>: Update an existing product by ID.<\/li>\n\n\n\n<li><strong>DELETE <code>\/products\/{id}<\/code><\/strong>: Delete a product by ID.<\/li>\n<\/ul>\n\n\n\n<p>This example demonstrates how to use .NET Core Minimal API to create REST endpoints with minimal code and complexity. This approach is particularly useful for small, simple APIs where a full MVC structure might be unnecessary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">References<\/h3>\n\n\n\n<p>Here are some useful web references that can help you increase your knowledge on creating REST endpoints using .NET Core Minimal API:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Microsoft Learn &#8211; Minimal APIs in .NET<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> Official Microsoft documentation that provides an introduction to Minimal APIs in .NET, including how to create and manage RESTful endpoints with this streamlined approach.<\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/minimal-apis?view=aspnetcore-6.0\">Microsoft Learn &#8211; Minimal APIs in .NET<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>ASP.NET Blog &#8211; Introduction to Minimal APIs<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A blog post from the ASP.NET team introducing Minimal APIs, including code examples and comparisons with traditional controllers.<\/li>\n\n\n\n<li><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/introducing-the-minimal-apis-in-asp-net-core-mvc\/\">ASP.NET Blog &#8211; Introduction to Minimal APIs<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Code Maze &#8211; Building REST APIs with Minimal API in .NET 6<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A detailed tutorial on building RESTful APIs using Minimal API in .NET 6, with step-by-step instructions and code examples.<\/li>\n\n\n\n<li><a>Code Maze &#8211; Building REST APIs with Minimal API<\/a><\/li>\n<\/ul>\n\n\n\n<p>These resources provide a comprehensive overview of Minimal APIs in .NET, helping you understand how to create and manage REST endpoints using this streamlined approach. Whether you&#8217;re just starting with Minimal APIs or looking to deepen your knowledge, these references will be invaluable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating REST endpoints using .NET Core Minimal API is a streamlined approach to building web APIs with less boilerplate code. Below is an example of how to create several REST endpoints using .NET Core Minimal API. This example assumes you are working with a simple model, such as a Product, and provides endpoints for basic [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"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":1,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":29,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/28\/revisions\/29"}],"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}]}}