{"id":6526,"date":"2020-07-11T14:40:47","date_gmt":"2020-07-11T14:40:47","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6526"},"modified":"2020-07-11T14:40:47","modified_gmt":"2020-07-11T14:40:47","slug":"asp-net-core-and-web-api","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/asp-net-core-and-web-api\/","title":{"rendered":"ASP.NET Core and Web API"},"content":{"rendered":"<p>In the past I&#8217;ve looked at ServiceStack for developing REST services however there are alternatives to ServiceStack, one of which is Web Api. Let&#8217;s create a simple starter project using ASP.NET Core and Web Api to implement our services.<\/p>\n<ul>\n<li>Create a new ASP.NET Core Web Application<\/li>\n<li>Select API<\/li>\n<\/ul>\n<p>The generated service supplies a Controllers\/ValuesController.cs file which includes a controller class with the various REST methods.<\/p>\n<p>Controllers partition your application&#8217;s API, so for example you might have an AuthenticateController solely for authentication methods and another controller for CRUD operations on a datasource. <\/p>\n<p>For example this is a simple AuthenticateController class <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;Route(&quot;&#x5B;controller]&quot;)]\r\n&#x5B;ApiController]\r\npublic class AuthenticateController : ControllerBase\r\n{\r\n   &#x5B;HttpPost]\r\n   public ActionResult&lt;AuthenticateReponse&gt; Post(&#x5B;FromBody] User user)\r\n   {\r\n      return new AuthenticateReponse\r\n      {\r\n         Token = $&quot;{user.Name}|1234&quot;\r\n      };\r\n   }\r\n}\r\n<\/pre>\n<p>Here, the Route is set to [controller] so our URL would be http:\/\/localhost\/Authenticate. In this example we intend for the user to send data within an HTTP POST method, passing a User object (which is a simple name\/password object). An AuthenticateResponse object is return via the ActionResult.<\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/web-api\/action-return-types?view=aspnetcore-2.1#actionresultt-type\" rel=\"noopener noreferrer\" target=\"_blank\">ActionResult<\/a> return value allows us to return HTTP 200 OK or using subclasses such as <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.aspnetcore.mvc.controllerbase.badrequest?view=aspnetcore-2.1#Microsoft_AspNetCore_Mvc_ControllerBase_BadRequest_Microsoft_AspNetCore_Mvc_ModelBinding_ModelStateDictionary_\" rel=\"noopener noreferrer\" target=\"_blank\">BadRequest<\/a> for an HTTP 400 and there are other derivations for other HTTP responses. <\/p>\n<p>The Action&lt;&gt; allows us to return a 200 OK without the need of using the Ok() method, although this can be used if preferred.<\/p>\n<p>When using different return responses it&#8217;s good to also document these by adding <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ProducesResponseType(400)]\r\n<\/pre>\n<p>This indicates that the method may return an HTTP 400. This is useful for use with Open API tools such as Swagger.<\/p>\n<p>As can be seen in the Post method, the User object is expected within the method&#8217;s body and the HttpPost attribute declare the method uses HTTP POST, the method name is superfluous in terms of the REST API.<\/p>\n<p><strong>The different HTTP methods<\/strong><\/p>\n<p>The code generated by the template creates a ValuesController with various HTTP methods, which I&#8217;ll recreate here for reference<\/p>\n<p><em>HTTP GET without URL parameters<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;HttpGet]\r\npublic ActionResult&lt;IEnumerable&lt;string&gt;&gt; Get()\r\n{\r\n   return new string&#x5B;] { &quot;value1&quot;, &quot;value2&quot; };\r\n}\r\n<\/pre>\n<p><em>HTTP GET with URL parameters<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;HttpGet(&quot;{id}&quot;)]\r\npublic ActionResult&lt;string&gt; Get(int id)\r\n{\r\n   return &quot;value&quot;;\r\n}\r\n<\/pre>\n<p><em>HTTP POST without URL parameters<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;HttpPost]\r\npublic void Post(&#x5B;FromBody] string value)\r\n{\r\n}\r\n<\/pre>\n<p><em>HTTP PUT with URL parameters<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;HttpPut(&quot;{id}&quot;)]\r\npublic void Put(int id, &#x5B;FromBody] string value)\r\n{\r\n}\r\n<\/pre>\n<p><em>HTTP DELETE with URL parameters<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;HttpDelete(&quot;{id}&quot;)]\r\npublic void Delete(int id)\r\n{\r\n}\r\n<\/pre>\n<p><em>HTTP HEAD<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;HttpGet, HttpHead]\r\npublic IActionResult Head()\r\n{\r\n   return Ok();\r\n}\r\n<\/pre>\n<p>We would request the headers using HttpHead along with an HttpGet.<\/p>\n<p><em>HTTP PATCH<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;HttpPatch]\r\npublic IActionResult Patch()\r\n{\r\n   return Ok();\r\n}\r\n<\/pre>\n<p><strong>Adding Swagger support<\/strong><\/p>\n<p>As per <a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/tutorials\/web-api-help-pages-using-swagger?view=aspnetcore-2.1\" rel=\"noopener noreferrer\" target=\"_blank\">ASP.NET Core Web API help pages with Swagger \/ OpenAPI<\/a> we can add Swagger support using either Swashbuckle.AspNetCore or NSwag (there may be others). Both are simple to add to our project.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the past I&#8217;ve looked at ServiceStack for developing REST services however there are alternatives to ServiceStack, one of which is Web Api. Let&#8217;s create a simple starter project using ASP.NET Core and Web Api to implement our services. Create a new ASP.NET Core Web Application Select API The generated service supplies a Controllers\/ValuesController.cs file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[200],"tags":[],"class_list":["post-6526","post","type-post","status-publish","format-standard","hentry","category-asp-net-core"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6526"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6526\/revisions"}],"predecessor-version":[{"id":8403,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6526\/revisions\/8403"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}