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 CRUD operations (Create, Read, Update, Delete).
Step 1: Set Up the Project
First, create a new .NET Core Minimal API project:
bashCopy codedotnet new web -n MinimalApiExample
cd MinimalApiExample
Step 2: Define the Model
Create a simple Product
class that will represent the data:
csharpCopy codepublic class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Step 3: Set Up the In-Memory Data Store
For simplicity, we’ll use an in-memory list to store the products:
csharpCopy codevar products = new List<Product>
{
new Product { Id = 1, Name = "Product 1", Price = 10.00m },
new Product { Id = 2, Name = "Product 2", Price = 20.00m },
new Product { Id = 3, Name = "Product 3", Price = 30.00m }
};
Step 4: Create the Minimal API Endpoints
In the Program.cs
file, define your Minimal API endpoints:
csharpCopy codevar builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var products = new List<Product>
{
new Product { Id = 1, Name = "Product 1", Price = 10.00m },
new Product { Id = 2, Name = "Product 2", Price = 20.00m },
new Product { Id = 3, Name = "Product 3", Price = 30.00m }
};
// Get all products
app.MapGet("/products", () =>
{
return Results.Ok(products);
});
// Get a product by ID
app.MapGet("/products/{id:int}", (int id) =>
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product is null)
{
return Results.NotFound();
}
return Results.Ok(product);
});
// Create a new product
app.MapPost("/products", (Product product) =>
{
product.Id = products.Max(p => p.Id) + 1;
products.Add(product);
return Results.Created($"/products/{product.Id}", product);
});
// Update an existing product
app.MapPut("/products/{id:int}", (int id, Product updatedProduct) =>
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product is null)
{
return Results.NotFound();
}
product.Name = updatedProduct.Name;
product.Price = updatedProduct.Price;
return Results.Ok(product);
});
// Delete a product
app.MapDelete("/products/{id:int}", (int id) =>
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product is null)
{
return Results.NotFound();
}
products.Remove(product);
return Results.NoContent();
});
app.Run();
Step 5: Run the Application
To run the application, use the following command:
bashCopy codedotnet run
Step 6: Test the Endpoints
You can test the endpoints using tools like curl
, Postman, or directly in a browser.
- Get all products:httpCopy code
GET /products
- Get a product by ID:httpCopy code
GET /products/1
- Create a new product:httpCopy code
POST /products Content-Type: application/json { "name": "New Product", "price": 15.50 }
- Update an existing product:httpCopy code
PUT /products/1 Content-Type: application/json { "name": "Updated Product", "price": 12.00 }
- Delete a product:httpCopy code
DELETE /products/1
Summary of REST Endpoints:
- GET
/products
: Retrieve all products. - GET
/products/{id}
: Retrieve a specific product by ID. - POST
/products
: Create a new product. - PUT
/products/{id}
: Update an existing product by ID. - DELETE
/products/{id}
: Delete a product by ID.
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.
References
Here are some useful web references that can help you increase your knowledge on creating REST endpoints using .NET Core Minimal API:
1. Microsoft Learn – Minimal APIs in .NET
- Overview: Official Microsoft documentation that provides an introduction to Minimal APIs in .NET, including how to create and manage RESTful endpoints with this streamlined approach.
- Microsoft Learn – Minimal APIs in .NET
2. ASP.NET Blog – Introduction to Minimal APIs
- Overview: A blog post from the ASP.NET team introducing Minimal APIs, including code examples and comparisons with traditional controllers.
- ASP.NET Blog – Introduction to Minimal APIs
3. Code Maze – Building REST APIs with Minimal API in .NET 6
- Overview: A detailed tutorial on building RESTful APIs using Minimal API in .NET 6, with step-by-step instructions and code examples.
- Code Maze – Building REST APIs with Minimal API
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’re just starting with Minimal APIs or looking to deepen your knowledge, these references will be invaluable.
Leave a Reply