{"id":1976,"date":"2021-06-05T12:00:00","date_gmt":"2021-06-05T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-create-rest-api-in-net-using-c-and-visual-studio\/"},"modified":"2026-07-05T03:25:37","modified_gmt":"2026-07-05T01:25:37","slug":"how-to-create-rest-api-in-net-using-c-and-visual-studio","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-create-rest-api-in-net-using-c-and-visual-studio\/","title":{"rendered":"How to Create REST API in .Net Using C# and Visual Studio"},"content":{"rendered":"<p>In this tutorial and subsequent ones, you will learn how to create a .NET REST API in C# using Visual Studio. In this first part, we would create a REST API that returns a list of friends.<\/p>\n<p>Next we would add the endpoints for INSERT, UPDATE and DELETE.<\/p>\n<p>Finally, we would generate the UI (Razor) pages using Visual Studio very easily.<\/p>\n<ol>\n<li><a href=\"#t1\">Setup the Project<\/a><\/li>\n<li><a href=\"#t2\">Create the Model and Controller<\/a><\/li>\n<li><a href=\"#t3\">Making a GET Request<\/a><\/li>\n<li><a href=\"#t4\">Making a POST Request<\/a><\/li>\n<li><a href=\"#t5\">PUT Request<\/a><\/li>\n<li><a href=\"#t6\">Delete Request<\/a><\/li>\n<li><a href=\"#t7\">Next Steps<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Setup the Project and Create the Model<\/strong><\/h4>\n<p>Start a new project in Visual Studio<\/p>\n<p>In the Project template window, choose ASP.Net Core Web Application as shown below:<\/p>\n<figure id=\"attachment_14643\" aria-describedby=\"caption-attachment-14643\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-ASP.Net-Core-Web-Application.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-14643\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-ASP.Net-Core-Web-Application-300x183.jpg\" alt=\"Choose ASP.Net Core Web Application\" width=\"300\" height=\"183\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-ASP.Net-Core-Web-Application-300x183.jpg 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-ASP.Net-Core-Web-Application-1024x626.jpg 1024w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-ASP.Net-Core-Web-Application-768x470.jpg 768w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-ASP.Net-Core-Web-Application-600x367.jpg 600w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-ASP.Net-Core-Web-Application.jpg 1300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-14643\" class=\"wp-caption-text\">Choose ASP.Net Core Web Application<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Give the project a name. I call it APITutorial.<\/p>\n<p>Click on Ok.<\/p>\n<p>In the next window, choose API as shown below:<\/p>\n<figure id=\"attachment_14642\" aria-describedby=\"caption-attachment-14642\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-API.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-14642\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-API-300x211.jpg\" alt=\"Choose API\" width=\"300\" height=\"211\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-API-300x211.jpg 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-API-1024x719.jpg 1024w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-API-768x540.jpg 768w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-API-120x85.jpg 120w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-API-600x422.jpg 600w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Choose-API.jpg 1180w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-14642\" class=\"wp-caption-text\">Choose API<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Click on OK<\/p>\n<p>Now, the project sets up. Then open the ValuesController.cs file which is inside the Controllers folder.\u00a0 You will see that a basic REST controller is set up with placeholders for GET, POST, PUT and DELETE.<\/p>\n<p>If you run the applications, you can visit the route https:\/\/localhost:44385\/api\/values. It returns a response:\u00a0 [&#8220;value1&#8221;, &#8220;value2&#8221;]<\/p>\n<p>So we now want to write the function that returns a list of friend.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Create the Model and Controller<\/strong><\/h4>\n<p>First create a class called Friend. This class would have four fields: firstname, lastname, location and dateOfHire.<\/p>\n<p>This class is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Friend<\/span>\r\n    {\r\n        <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> firstname { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }\r\n        <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> lastname { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }\r\n        <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> location { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }\r\n        <span style=\"color: #008800; font-weight: bold;\">private<\/span> DateTime dateOfHire { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }\r\n\r\n        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Friend<\/span>()\r\n        {\r\n        }\r\n\r\n        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Friend<\/span>(<span style=\"color: #333399; font-weight: bold;\">string<\/span> firstname, <span style=\"color: #333399; font-weight: bold;\">string<\/span> lastname, <span style=\"color: #333399; font-weight: bold;\">string<\/span> location, DateTime dateOfHire)\r\n        {\r\n            <span style=\"color: #008800; font-weight: bold;\">this<\/span>.firstname = firstname;\r\n            <span style=\"color: #008800; font-weight: bold;\">this<\/span>.lastname = lastname;\r\n            <span style=\"color: #008800; font-weight: bold;\">this<\/span>.location = location;\r\n            <span style=\"color: #008800; font-weight: bold;\">this<\/span>.dateOfHire = dateOfHire;\r\n        }\r\n    }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>We then need to create the controller class inside the controllers folder.<\/p>\n<p>Right-click on the controller folder and choose Add New Controller.<\/p>\n<p>Choose API Controller in the Add Scaffold window as shown below:<\/p>\n<figure id=\"attachment_14648\" aria-describedby=\"caption-attachment-14648\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Creating-a-Controller-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-14648\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Creating-a-Controller-2-300x155.jpg\" alt=\"Creating a REST API Controller\" width=\"300\" height=\"155\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Creating-a-Controller-2-300x155.jpg 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Creating-a-Controller-2-1024x529.jpg 1024w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Creating-a-Controller-2-768x397.jpg 768w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Creating-a-Controller-2-600x310.jpg 600w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Creating-a-Controller-2.jpg 1287w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-14648\" class=\"wp-caption-text\">Creating a REST API Controller<\/figcaption><\/figure>\n<p>Click on Add.<\/p>\n<p>In the next window, give it a name FriendController<\/p>\n<p>Then ok.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Making a GET Request<\/strong><\/h4>\n<p>You&#8217;ll see that the controller file is generated with some content. Adjust the code in the get section to the following:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ GET: api\/Friend<\/span>\r\n<span style=\"color: #0000cc;\">[HttpGet]<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List&lt;Friend&gt; Get()\r\n{\r\n    List&lt;Friend&gt; friends = <span style=\"color: #008800; font-weight: bold;\">new<\/span> List&lt;Friend&gt;();\r\n    friends.Add(<span style=\"color: #008800; font-weight: bold;\">new<\/span> Friend(<span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Munonye\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Budapest\"<\/span>, DateTime.Today));\r\n    friends.Add(<span style=\"color: #008800; font-weight: bold;\">new<\/span> Friend(<span style=\"background-color: #fff0f0;\">\"Oleander\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Yuba\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Nigeria\"<\/span>, DateTime.Today));\r\n    friends.Add(<span style=\"color: #008800; font-weight: bold;\">new<\/span> Friend(<span style=\"background-color: #fff0f0;\">\"Saffron\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Lawrence\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Lagos\"<\/span>, DateTime.Today));\r\n    friends.Add(<span style=\"color: #008800; font-weight: bold;\">new<\/span> Friend(<span style=\"background-color: #fff0f0;\">\"Jadon\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Munonye\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Asaba\"<\/span>, DateTime.Today));\r\n    friends.Add(<span style=\"color: #008800; font-weight: bold;\">new<\/span> Friend(<span style=\"background-color: #fff0f0;\">\"Solace\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Okeke\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Oko\"<\/span>, DateTime.Today));\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> friends;\r\n}\r\n<\/pre>\n<p>Now you can run the application and access \/api\/friend<\/p>\n<p>In the case of findById, use the code below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ GET: api\/Friend\/5<\/span>\r\n<span style=\"color: #0000cc;\">[HttpGet(\"{id}\", Name = \"Get\")]<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Friend <span style=\"color: #0066bb; font-weight: bold;\">Get<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> id)\r\n{\r\n    Friend friend = friends.Find(f =&gt; f.id == id);\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> friend;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Making a POST Request<\/strong><\/h4>\n<p>To make POST request, we need to modify the POST section of the controller file.<\/p>\n<p>Since we are not using database yet, we would simply add the received friend object to the list and return the new list. The code is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ POST: api\/Friend<\/span>\r\n<span style=\"color: #0000cc;\">[HttpPost]<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List&lt;Friend&gt; Post([FromBody] Friend friend)\r\n{\r\n    friends.Add(friend);\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> friends;\r\n}\r\n<\/pre>\n<p>You can test is to see. You can use either Postman or Advanced Rest Client(ARC).<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. PUT Request<\/strong><\/h4>\n<p>Here, we would receive a friend object from the body of the PUT request. We&#8217;ll also receive the id in the URL.\u00a0 Then we&#8217;ll do the following:<\/p>\n<ul>\n<li>find to find the specified item in the list<\/li>\n<li>update the item using the data in the request body<\/li>\n<li>return the new list<\/li>\n<\/ul>\n<p>The code is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ PUT: api\/Friend\/5<\/span>\r\n<span style=\"color: #0000cc;\">[HttpPut(\"{id}\")]<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List&lt;Friend&gt; Put(<span style=\"color: #333399; font-weight: bold;\">int<\/span> id, [FromBody] Friend friend)\r\n{\r\n    Friend friendToUpdate = friends.Find(f =&gt; f.id == id);\r\n    <span style=\"color: #333399; font-weight: bold;\">int<\/span> index = friends.IndexOf(friendToUpdate);\r\n\r\n    friends[index].firstname = friend.firstname;\r\n    friends[index].lastname = friend.lastname;\r\n    friends[index].location = friend.location;\r\n    friends[index].dateOfHire = friend.dateOfHire;\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> friends;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Delete Request<\/strong><\/h4>\n<p>In this case you simply find the item with the specified id.<\/p>\n<p>Delete it and then return the modified list. The code is given below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ DELETE: api\/ApiWithActions\/5<\/span>\r\n<span style=\"color: #0000cc;\">[HttpDelete(\"{id}\")]<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List&lt;Friend&gt; Delete(<span style=\"color: #333399; font-weight: bold;\">int<\/span> id)\r\n{\r\n    Friend friend = friends.Find(f =&gt; f.id == id);\r\n    friends.Remove(friend);\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> friends;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t7\">7. Next Steps &#8211; MVC and Razor<\/strong><\/h4>\n<p>If you have successfully completed this tutorial, then thumbs up to you! However there&#8217;s yet much to be done.<\/p>\n<p>Therefore we would cover the following in subsequent tutorials:<\/p>\n<ul>\n<li>Connect to a MS SQL Database<\/li>\n<li>Use Entity Framework<\/li>\n<li>Save data to database<\/li>\n<li>Create an view using Razor and HTML<\/li>\n<\/ul>\n<p>Do check the video series for a hand-on step by step and more explanation here in <a href=\"https:\/\/www.youtube.com\/c\/KindsonTheTechPro\" target=\"_blank\" rel=\"noopener\">my YouTube Channel<\/a>. And do remember to subscribe!<\/p>\n<p>Happy learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial and subsequent ones, you will learn how to create a .NET REST API in C# using Visual Studio. In this first part, &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[289],"tags":[],"class_list":["post-1976","post","type-post","status-publish","format-standard","hentry","category-rest-web-services"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1976"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1976\/revisions"}],"predecessor-version":[{"id":2144,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1976\/revisions\/2144"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}