Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Anand PandeyRank #729 · 3.2K reputationPosted Oct 23, 2025, 12:37 PM EDT
Create a Microservice in .NET
Create project:
dotnet new webapi -n ProductService cd ProductServiceAdd model (Product.cs):
public class Product { public int Id; public string Name; public decimal Price; }Add controller (ProductsController.cs):
[ApiController] [Route("api/[controller]")] public class ProductsController : ControllerBase { static List products = new() { new() { Id=1, Name="Phone", Price=699 }, new() { Id=2, Name="Laptop", Price=1299 } }; [HttpGet] public IActionResult Get() => Ok(products); } Run it:
dotnet run?? Visit
https://localhost:5001/api/productsAnand PandeyRank #729 · 3.2K reputationPosted Oct 23, 2025, 12:41 PM EDT
A small, independent service that handles one specific function (like products, orders, or users) and communicates via APIs.
2. How to Create a Microservice in .NET?
Run this in terminal:
dotnet new webapi -n ProductService cd ProductService3. Add a Model (
Product.cs)public class Product { public int Id; public string Name; public decimal Price; }4. Add a Controller (
ProductsController.cs)[ApiController] [Route("api/[controller]")] public class ProductsController : ControllerBase { static List products = new() { new() { Id=1, Name="Phone", Price=699 }, new() { Id=2, Name="Laptop", Price=1299 } }; [HttpGet] public IActionResult Get() => Ok(products); } 5. Run the Microservice
dotnet runOpen ??
https://localhost:5001/api/products