{"id":5030,"date":"2024-02-21T20:08:41","date_gmt":"2024-02-21T20:08:41","guid":{"rendered":"https:\/\/reviewnprep.com\/blog\/?p=5030"},"modified":"2025-09-09T01:29:53","modified_gmt":"2025-09-09T01:29:53","slug":"a-comprehensive-guide-on-creating-apis-in-c","status":"publish","type":"post","link":"https:\/\/reviewnprep.com\/blog\/a-comprehensive-guide-on-creating-apis-in-c\/","title":{"rendered":"A Comprehensive Guide on Creating APIs in C#"},"content":{"rendered":"\n<p>In the realm of modern software development, creating APIs (Application Programming Interfaces) is essential for facilitating communication between different software systems. In this blog post, we&#8217;ll delve into the process of creating APIs in C# using the ASP.NET framework. <\/p>\n\n\n\n<p>We&#8217;ll cover the basics of API development, including the implementation of HTTP methods such as GET, POST, PUT, and DELETE. Additionally, we&#8217;ll explore the distinctions between APIs and web services, and provide a practical example that can be tested using Postman. <\/p>\n\n\n\n<p>Let&#8217;s dive in!<\/p>\n\n\n\n<p>APIs (Application Programming Interfaces) allow different software systems to communicate with each other. APIs work by exposing endpoints that can be called by other programs. In this post, we&#8217;ll look at how to create a basic API using C# and ASP.NET Core. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an API?<\/h2>\n\n\n\n<p>An API is a set of definitions and protocols for <a href=\"https:\/\/tacetra.com\/blogs\/future-proof-career-automation\/\" target=\"_blank\" rel=\"noreferrer noopener\">building and integrating application software<\/a>. APIs let your product or service communicate with other products and services without having to know how they&#8217;re implemented. This allows you to open up data and functionality to other developers and to other systems.<\/p>\n\n\n\n<p>APIs allow you to securely expose certain parts of your application logic to the outside world in a controlled manner. For example, the Twitter API allows other developers to integrate Twitter data into their own applications, under certain conditions. APIs are the backbone of the interconnected web services we use every day.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding APIs and Web Services<\/h2>\n\n\n\n<p>Before we delve into the technical aspects of API development, it&#8217;s crucial to grasp the distinction between APIs and web services. While both enable communication between software systems, they differ in their implementation and scope.<\/p>\n\n\n\n<p><strong>APIs<\/strong> serve as interfaces that allow different software applications to interact with each other. They define the methods and protocols that developers can use to access the functionalities of a software system.<\/p>\n\n\n\n<p><strong>Web services<\/strong>, on the other hand, are a specific type of API that operates over the web using standard protocols such as HTTP. They typically follow the principles of REST (Representational State Transfer) or SOAP (Simple Object Access Protocol).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up the Development Environment<\/h2>\n\n\n\n<p>To create APIs in C#, you&#8217;ll need the following prerequisites:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Visual Studio Code IDE<\/strong>: To code any software you need an IDE. One of the most popular integrated development environment (IDE) for building .NET applications is Visual Studio Code. You can install the application by going to the <a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">official VS Code link from Microsoft<\/a>. <\/li>\n\n\n\n<li><strong>ASP.NET Core SDK<\/strong>: Ensure that you have the ASP.NET Core SDK installed, as it provides the necessary tools for developing web applications with C#. You can download it from the <a href=\"https:\/\/dotnet.microsoft.com\/en-us\/download\" target=\"_blank\" rel=\"noreferrer noopener\">official .NET link from Microsoft<\/a>.<\/li>\n\n\n\n<li><strong>Postman<\/strong>: Finally, download and install Postman, a popular API development and testing tool. You&#8217;d need Postman to call the various endpoints that we will code in this tutorial. You can <a href=\"https:\/\/www.postman.com\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener\">download Postman from the official link<\/a>.<\/li>\n\n\n\n<li>Basic knowledge of C# and .NET Framework is needed.<\/li>\n<\/ol>\n\n\n\n<p>It is important to note that VS Code doesn&#8217;t include a traditional&nbsp;<strong>File<\/strong>&nbsp;&gt;&nbsp;<strong>New Project<\/strong>&nbsp;dialog or pre-installed project templates. You&#8217;ll need to add&nbsp;<a href=\"https:\/\/code.visualstudio.com\/docs\/setup\/additional-components\" target=\"_blank\" rel=\"noreferrer noopener\">additional components<\/a>&nbsp;and scaffolders depending on your development interests.&nbsp;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Pro-Tip: You can install the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-dotnettools.csdevkit&amp;WT.mc_id=dotnet-35129-website\" target=\"_blank\" rel=\"noreferrer noopener\">C# Dev Kit<\/a> within VS Studio Code to&nbsp;test your code with integrated unit test discovery and execution<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the API Project<\/h2>\n\n\n\n<p>Let&#8217;s start by creating a new ASP.NET Web Application project in Visual Studio Code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a New Folder<\/strong>: Create a new folder for your C# project using Visual Studio Code&#8217;s file explorer or in your terminal\/command prompt.<\/li>\n\n\n\n<li><strong>Initialize a new C# Project<\/strong>: Open the terminal in Visual Studio Code (press `Ctrl+&#8220;) and navigate to your project folder. You can also navigate to Terminal option on the top ribbon of your VS code and click on New Terminal. Run the following command to initialize a new C# project:<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"dotnet new webapi -n MyApiProject\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F69D50\">dotnet<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">new<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">webapi<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">-n<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">MyApiProject<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This will create a new project with the required references and packages needed to build a basic Web API. The folder structure should look something like below: <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"424\" src=\"https:\/\/reviewnprep.com\/blog\/wp-content\/uploads\/2024\/02\/image.png\" alt=\"\" class=\"wp-image-5033\" style=\"aspect-ratio:1.509433962264151;width:309px;height:auto\" srcset=\"https:\/\/reviewnprep.com\/blog\/wp-content\/uploads\/2024\/02\/image.png 640w, https:\/\/reviewnprep.com\/blog\/wp-content\/uploads\/2024\/02\/image-300x199.png 300w, https:\/\/reviewnprep.com\/blog\/wp-content\/uploads\/2024\/02\/image-100x66.png 100w, https:\/\/reviewnprep.com\/blog\/wp-content\/uploads\/2024\/02\/image-150x99.png 150w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Adding a Model<\/h3>\n\n\n\n<p>Under the project folder, create a new <code>Models<\/code> folder and add a <code>Product.cs<\/code> class:<\/p>\n\n\n\n<p>Now let&#8217;s add a simple <code>Product<\/code> model that we will use in our API:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"namespace MyApiProject.Models\n{\n    public class Product\n    {\n          public Product() \n            {\n                Name = &quot;&quot;; \n            }\n\n        public int Id { get; set; }\n        public string Name { get; set; }\n        public decimal Price { get; set; }\n    }\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">namespace<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">MyApiProject<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">Models<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">class<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Product<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">          <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Product<\/span><span style=\"color: #ADBAC7\">() <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">            {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">                Name <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;&quot;<\/span><span style=\"color: #ADBAC7\">; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">            }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Id<\/span><span style=\"color: #ADBAC7\"> { <\/span><span style=\"color: #F47067\">get<\/span><span style=\"color: #ADBAC7\">; <\/span><span style=\"color: #F47067\">set<\/span><span style=\"color: #ADBAC7\">; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">string<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Name<\/span><span style=\"color: #ADBAC7\"> { <\/span><span style=\"color: #F47067\">get<\/span><span style=\"color: #ADBAC7\">; <\/span><span style=\"color: #F47067\">set<\/span><span style=\"color: #ADBAC7\">; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">decimal<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Price<\/span><span style=\"color: #ADBAC7\"> { <\/span><span style=\"color: #F47067\">get<\/span><span style=\"color: #ADBAC7\">; <\/span><span style=\"color: #F47067\">set<\/span><span style=\"color: #ADBAC7\">; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This <code>Product<\/code> class will act as our data model that we will perform CRUD operations on.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the Controller<\/h3>\n\n\n\n<p>The main logic of our API will reside in the controller. Let&#8217;s add a <code>ProductsController<\/code> under the Controllers folder:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"using Microsoft.AspNetCore.Mvc;\nusing MyApiProject.Models;\n\nnamespace MyApiProject.Controllers\n{\n    [Route(&quot;api\/[controller]&quot;)]\n    [ApiController]\n    public class ProductsController : ControllerBase\n    {\n        \/\/ Controller methods (We will add the CRUD Logic here)\n    }\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">using<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Microsoft<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">AspNetCore<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">Mvc<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">using<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">MyApiProject<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">Models<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">namespace<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">MyApiProject<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">Controllers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    [<\/span><span style=\"color: #F69D50\">Route<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;api\/[controller]&quot;<\/span><span style=\"color: #ADBAC7\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    [<\/span><span style=\"color: #F69D50\">ApiController<\/span><span style=\"color: #ADBAC7\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">class<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">ProductsController<\/span><span style=\"color: #ADBAC7\"> : <\/span><span style=\"color: #F69D50\">ControllerBase<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">        \/\/ Controller methods (We will add the CRUD Logic here)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>We decorate the class with <code>[Route]<\/code> and <code>[ApiController]<\/code> attributes to mark it as a controller that will handle API requests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding CRUD Methods<\/h2>\n\n\n\n<p>Now let&#8217;s add methods for CRUD operations:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Get All Products<\/h3>\n\n\n\n<p><strong>GET Method<\/strong> &#8211; The GET method in API is used to request data from a specified resource. When a client sends a GET request to a server, it is asking for information from the server. This method is used for retrieving data and should not have any other effect on the data or server state. <\/p>\n\n\n\n<p>GET requests are typically used for operations such as reading or fetching data, and they do not have a request body. In RESTful APIs, GET requests are often used to retrieve resource representations, such as retrieving a list of items or fetching a specific item by its unique identifier. <\/p>\n\n\n\n<p>Let&#8217;s continue with the example of adding a simple GET method to get Products<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ GET: api\/products\n[HttpGet]\npublic IActionResult GetProducts()\n{\n    \/\/ Return list of products (dummy data)\n    return Ok(new List&lt;Product&gt;\n    {\n        new Product { Id = 1, Name = &quot;Apple&quot;, Price = 1.99M },\n        new Product { Id = 2, Name = &quot;Orange&quot;, Price = 2.99M }, \n    });\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\">\/\/ GET: api\/products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">[<\/span><span style=\"color: #F69D50\">HttpGet<\/span><span style=\"color: #ADBAC7\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">GetProducts<\/span><span style=\"color: #ADBAC7\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ Return list of products (dummy data)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Ok<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">List<\/span><span style=\"color: #ADBAC7\">&lt;<\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        <\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\"> { Id <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">1<\/span><span style=\"color: #ADBAC7\">, Name <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;Apple&quot;<\/span><span style=\"color: #ADBAC7\">, Price <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">1.99M<\/span><span style=\"color: #ADBAC7\"> },<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        <\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\"> { Id <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">2<\/span><span style=\"color: #ADBAC7\">, Name <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;Orange&quot;<\/span><span style=\"color: #ADBAC7\">, Price <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">2.99M<\/span><span style=\"color: #ADBAC7\"> }, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    });<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This method handles GET request to <code>\/api\/products<\/code> and returns a list of products.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Get Single Product<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ GET: api\/products\/5\n[HttpGet(&quot;{id}&quot;)]\npublic IActionResult GetProduct(int id)\n{\n    \/\/ Return single product (dummy data)\n    var product = new Product { Id = id, Name = &quot;Tomato&quot;, Price = 3.99M };\n    \n    return Ok(product);\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\">\/\/ GET: api\/products\/5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">[<\/span><span style=\"color: #F69D50\">HttpGet<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;{id}&quot;<\/span><span style=\"color: #ADBAC7\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">GetProduct<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">id<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ Return single product (dummy data)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">var<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">product<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\"> { Id <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> id, Name <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;Tomato&quot;<\/span><span style=\"color: #ADBAC7\">, Price <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">3.99M<\/span><span style=\"color: #ADBAC7\"> };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Ok<\/span><span style=\"color: #ADBAC7\">(product);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This method takes an <code>id<\/code> parameter from route and returns a single product object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Product<\/h3>\n\n\n\n<p><strong>Post<\/strong> <strong>Method<\/strong> &#8211; The POST method is used to submit data to be processed to a specified resource. When a client sends a POST request to a server, it is requesting the server to accept and store the enclosed entity, usually as a new resource or as an update to an existing resource.<\/p>\n\n\n\n<p>POST requests often include a request body containing data that the server needs to process. Common use cases for POST requests include creating new resources, submitting form data, and performing actions that result in state changes on the server.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ POST: api\/products\n[HttpPost]\npublic IActionResult CreateProduct([FromBody] Product product)\n{\n    \/\/ Create product logic here\n    \n    return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product); \n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\">\/\/ POST: api\/products<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">[<\/span><span style=\"color: #F69D50\">HttpPost<\/span><span style=\"color: #ADBAC7\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">CreateProduct<\/span><span style=\"color: #ADBAC7\">([<\/span><span style=\"color: #F69D50\">FromBody<\/span><span style=\"color: #ADBAC7\">] <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">product<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ Create product logic here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">CreatedAtAction<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">nameof<\/span><span style=\"color: #ADBAC7\">(GetProduct), <\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> { id <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> product.Id }, product); <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Here we handle POST request, take <code>Product<\/code> object from request body, create product and return 201 response.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Update Product<\/h3>\n\n\n\n<p><strong>Put Method: <\/strong>The PUT method is used to update or replace a resource identified by a specific URI (Uniform Resource Identifier). When a client sends a PUT request to a server, it is requesting the server to update the resource at the given URI with the enclosed entity in the request body.<\/p>\n\n\n\n<p>PUT requests are idempotent, meaning that multiple identical requests should have the same effect as a single request. PUT requests are commonly used for updating existing resources with new data provided in the request body.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ PUT: api\/products\/5\n[HttpPut(&quot;{id}&quot;)]\npublic IActionResult UpdateProduct(int id, [FromBody] Product product)\n{\n    \/\/ Update product logic here\n    \n    return NoContent();\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\">\/\/ PUT: api\/products\/5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">[<\/span><span style=\"color: #F69D50\">HttpPut<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;{id}&quot;<\/span><span style=\"color: #ADBAC7\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">UpdateProduct<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">id<\/span><span style=\"color: #ADBAC7\">, [<\/span><span style=\"color: #F69D50\">FromBody<\/span><span style=\"color: #ADBAC7\">] <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">product<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ Update product logic here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">NoContent<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This method handles PUT request, takes <code>id<\/code> from route and updated <code>Product<\/code> object from body and updates the product.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Delete Product<\/h3>\n\n\n\n<p><strong>DELETE Method: <\/strong>The DELETE method is used to request the removal of a specific resource from the server. When a client sends a DELETE request to a server, it is asking the server to delete the resource identified by the specified URI.<\/p>\n\n\n\n<p>DELETE requests are idempotent, meaning that making the same request multiple times should have the same effect as a single request. DELETE requests are typically used to delete resources that are no longer needed or to perform cleanup operations.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ DELETE: api\/products\/5 \n[HttpDelete(&quot;{id}&quot;)]\npublic IActionResult DeleteProduct(int id)\n{\n    \/\/ Delete product logic here\n    \n    return NoContent();\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\">\/\/ DELETE: api\/products\/5 <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">[<\/span><span style=\"color: #F69D50\">HttpDelete<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;{id}&quot;<\/span><span style=\"color: #ADBAC7\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">DeleteProduct<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">id<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ Delete product logic here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">NoContent<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This method handles DELETE request, takes <code>id<\/code> from route and deletes the product.<\/p>\n\n\n\n<p>That completes the basic CRUD methods for our API!<\/p>\n\n\n\n<p>Here is the complete code for ProductsController.cs with some sample data<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"using Microsoft.AspNetCore.Mvc;\nusing MyApiProject.Models;\n\nnamespace MyApiProject.Controllers\n{\n  [Route(&quot;api\/[controller]&quot;)]\n  [ApiController]\n  public class ProductsController : ControllerBase\n  {\n    \/\/ GET All \n    [HttpGet]\n    public IActionResult GetProducts()\n    {\n      return Ok(GetSampleProducts());\n    }\n\n    \/\/ GET by Id\n    [HttpGet(&quot;{id}&quot;)]\n    public IActionResult GetProduct(int id)\n    {\n      return Ok(GetSampleProducts().FirstOrDefault(x =&gt; x.Id == id));\n    }\n\n    \/\/ POST\n    [HttpPost]\n    public IActionResult CreateProduct([FromBody] Product product)\n    {\n      \/\/ Save product\n      return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product);\n    }\n\n    \/\/ PUT\n    [HttpPut(&quot;{id}&quot;)]\n    public IActionResult UpdateProduct(int id, [FromBody] Product product)\n    {\n      \/\/ Update product \n      return NoContent();\n    }\n\n    \/\/ DELETE\n    [HttpDelete(&quot;{id}&quot;)]\n    public IActionResult DeleteProduct(int id)\n    {\n      \/\/ Delete product\n      return NoContent(); \n    }\n\n    \/\/ Sample data\n    List&lt;Product&gt; GetSampleProducts()\n    {\n      return new List&lt;Product&gt;()\n      {\n        new Product() { Id = 1, Name = &quot;Apple&quot;, Price = 1.50M },\n        new Product() { Id = 2, Name = &quot;Orange&quot;, Price = 2.50M },\n      };\n    }\n  }\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">using<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Microsoft<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">AspNetCore<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">Mvc<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">using<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">MyApiProject<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">Models<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">namespace<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">MyApiProject<\/span><span style=\"color: #ADBAC7\">.<\/span><span style=\"color: #F69D50\">Controllers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  [<\/span><span style=\"color: #F69D50\">Route<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;api\/[controller]&quot;<\/span><span style=\"color: #ADBAC7\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  [<\/span><span style=\"color: #F69D50\">ApiController<\/span><span style=\"color: #ADBAC7\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">class<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">ProductsController<\/span><span style=\"color: #ADBAC7\"> : <\/span><span style=\"color: #F69D50\">ControllerBase<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ GET All <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    [<\/span><span style=\"color: #F69D50\">HttpGet<\/span><span style=\"color: #ADBAC7\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">GetProducts<\/span><span style=\"color: #ADBAC7\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Ok<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #DCBDFB\">GetSampleProducts<\/span><span style=\"color: #ADBAC7\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ GET by Id<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    [<\/span><span style=\"color: #F69D50\">HttpGet<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;{id}&quot;<\/span><span style=\"color: #ADBAC7\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">GetProduct<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">id<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Ok<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #DCBDFB\">GetSampleProducts<\/span><span style=\"color: #ADBAC7\">().<\/span><span style=\"color: #DCBDFB\">FirstOrDefault<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F69D50\">x<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> x.Id <\/span><span style=\"color: #F47067\">==<\/span><span style=\"color: #ADBAC7\"> id));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ POST<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    [<\/span><span style=\"color: #F69D50\">HttpPost<\/span><span style=\"color: #ADBAC7\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">CreateProduct<\/span><span style=\"color: #ADBAC7\">([<\/span><span style=\"color: #F69D50\">FromBody<\/span><span style=\"color: #ADBAC7\">] <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">product<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">      \/\/ Save product<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">CreatedAtAction<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">nameof<\/span><span style=\"color: #ADBAC7\">(GetProduct), <\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> { id <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> product.Id }, product);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ PUT<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    [<\/span><span style=\"color: #F69D50\">HttpPut<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;{id}&quot;<\/span><span style=\"color: #ADBAC7\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">UpdateProduct<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">id<\/span><span style=\"color: #ADBAC7\">, [<\/span><span style=\"color: #F69D50\">FromBody<\/span><span style=\"color: #ADBAC7\">] <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">product<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">      \/\/ Update product <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">NoContent<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ DELETE<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    [<\/span><span style=\"color: #F69D50\">HttpDelete<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;{id}&quot;<\/span><span style=\"color: #ADBAC7\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">IActionResult<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">DeleteProduct<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">id<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #768390\">      \/\/ Delete product<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">NoContent<\/span><span style=\"color: #ADBAC7\">(); <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\">    \/\/ Sample data<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F69D50\">List<\/span><span style=\"color: #ADBAC7\">&lt;<\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\">&gt; <\/span><span style=\"color: #DCBDFB\">GetSampleProducts<\/span><span style=\"color: #ADBAC7\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">List<\/span><span style=\"color: #ADBAC7\">&lt;<\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\">&gt;()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        <\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\">() { Id <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">1<\/span><span style=\"color: #ADBAC7\">, Name <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;Apple&quot;<\/span><span style=\"color: #ADBAC7\">, Price <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">1.50M<\/span><span style=\"color: #ADBAC7\"> },<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        <\/span><span style=\"color: #F47067\">new<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Product<\/span><span style=\"color: #ADBAC7\">() { Id <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">2<\/span><span style=\"color: #ADBAC7\">, Name <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;Orange&quot;<\/span><span style=\"color: #ADBAC7\">, Price <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">2.50M<\/span><span style=\"color: #ADBAC7\"> },<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Changes to Program.cs<\/h3>\n\n\n\n<p>Finally, add code to set up the web host and map the Controllers for routing.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#22272e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"using Microsoft.AspNetCore.Mvc;\nusing MyApiProject.Models;\n\nvar builder = WebApplication.CreateBuilder(args);\n\n\/\/ Add services to the container.\nbuilder.Services.AddControllers(); \n\nvar app = builder.Build();\n\n\/\/ Configure the HTTP request pipeline.\napp.UseHttpsRedirection();\n\napp.UseAuthorization();\n\napp.MapControllers(); \n\napp.Run();\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F69D50\">using<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">Microsoft.AspNetCore.Mvc<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F69D50\">using<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">MyApiProject.Models<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F69D50\">var<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">builder<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">WebApplication.CreateBuilder<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F69D50\">args<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F69D50\">\/\/<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">Add<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">services<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">to<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">the<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">container.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCBDFB\">builder.Services.AddControllers<\/span><span style=\"color: #ADBAC7\">(); <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F69D50\">var<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">app<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">builder.Build<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F69D50\">\/\/<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">Configure<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">the<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">HTTP<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">request<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">pipeline.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCBDFB\">app.UseHttpsRedirection<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCBDFB\">app.UseAuthorization<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCBDFB\">app.MapControllers<\/span><span style=\"color: #ADBAC7\">(); <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #DCBDFB\">app.Run<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Build and Run<\/h2>\n\n\n\n<p>Before we test the API, let&#8217;s do a build and run. Build compiles the code and checks for the errors, run command starts the localhost server. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use&nbsp;<code>dotnet build<\/code>&nbsp;to compile code<\/li>\n\n\n\n<li>Use&nbsp;<code>dotnet run<\/code>&nbsp;to start local server<\/li>\n<\/ul>\n\n\n\n<p>When do do the run, remember to check the terminal to see which port the localhost is using. This can be changed in your project in the launchSettings.json file. For example, my project is using port 5164 by default.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the API<\/h2>\n\n\n\n<p>Let&#8217;s test the API quickly using Postman.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Launch the API project from Visual Studio. This will run the API at&nbsp;<code>http:\/\/localhost:{port}\/api\/products<\/code><\/li>\n\n\n\n<li>Open Postman and create a new request.<\/li>\n\n\n\n<li>Select GET method and enter the URL&nbsp;<code>http:\/\/localhost:{port}\/api\/products<\/code>&nbsp;to test GetProducts API. This should return a list of products.<\/li>\n\n\n\n<li>Similarly, you can test other APIs by passing appropriate URLs and parameters.<\/li>\n<\/ul>\n\n\n\n<p>And that&#8217;s it! In this way, you can quickly build and test a basic Web API in C# and .NET Framework using Visual Studio.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>In this blog post, we&#8217;ve explored the process of creating APIs in C# using ASP.NET Core. We&#8217;ve covered the basics of API development, explained the differences between APIs and web services, and provided a practical example with CRUD operations for a book API. <\/p>\n\n\n\n<p>By following these steps and leveraging the power of C# and ASP.NET Core, you can develop robust APIs to facilitate seamless communication between your software systems. <\/p>\n\n\n\n<p>Happy coding!<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Further Reading: <\/p>\n\n\n\n<p><a href=\"https:\/\/reviewnprep.com\/blog\/ai-for-absolute-beginners-no-coding-required\/\" target=\"_blank\" rel=\"noreferrer noopener\">AI for Absolute Beginners: No Coding Required<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/reviewnprep.com\/blog\/navigating-the-job-market-most-in-demand-tech-skills-in-2024\/\" target=\"_blank\" rel=\"noreferrer noopener\">Navigating the Job Market: Most In-Demand Tech Skills in 2024<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial explains steps by step on how to create an API using C# and .NET. Use Visual Studio Code and Postman to test the API.<\/p>\n","protected":false},"author":1,"featured_media":5040,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[253],"tags":[388,323,363],"class_list":["post-5030","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-c-3","tag-development","tag-software-engineer"],"_links":{"self":[{"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/posts\/5030"}],"collection":[{"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/comments?post=5030"}],"version-history":[{"count":8,"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/posts\/5030\/revisions"}],"predecessor-version":[{"id":7020,"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/posts\/5030\/revisions\/7020"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/media\/5040"}],"wp:attachment":[{"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/media?parent=5030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/categories?post=5030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reviewnprep.com\/blog\/wp-json\/wp\/v2\/tags?post=5030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}