{"id":155,"date":"2020-02-21T14:22:42","date_gmt":"2020-02-21T05:22:42","guid":{"rendered":"http:\/\/sumomo.ohwaki.jp\/wordpress\/?p=155"},"modified":"2020-02-21T14:25:19","modified_gmt":"2020-02-21T05:25:19","slug":"asp-net-core-webapi","status":"publish","type":"post","link":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=155","title":{"rendered":"asp.net core webapi"},"content":{"rendered":"\n<p>ASP.NET Core\u306b\u306fRESTful Web API\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u3002<br>\u4f5c\u6210\u3059\u308b\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u3057\u3066\u3001webapi\u3092\u6307\u5b9a\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dotnet new <span style=\"font-weight:bold;color:red\">webapi<\/span> -o \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d5\u30a9\u30eb\u30c0<\/code><\/pre>\n\n\n\n<p>ASP.NET Core\u306eWeb API\u306fhttp\u8981\u6c42\u3092\u53d7\u3051\u53d6\u3063\u3066\u3001\u7d50\u679c\u3092JSON\u5f62\u5f0f\u3067\u8fd4\u3059\u3002<\/p>\n\n\n\n<p>API\u306e\u5b9f\u88c5\u306b\u306fhttp\u8981\u6c42\u306e\u30e1\u30bd\u30c3\u30c9\u6bce\u306b\u547c\u3073\u51fa\u3059\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3059\u308b\u3060\u3051\u3067\u826f\u3044\u3002<\/p>\n\n\n\n<p>\u7c21\u5358\u306a\u4f8b<\/p>\n\n\n\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d5\u30a9\u30eb\u30c0\u4e0b\u306eControllers\u30d5\u30a9\u30eb\u30c0\u306b&lt;\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u540d&gt;Controller.cs\u3068\u8a00\u3046\u5f62\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3068\u3001&#8221;http:\/\/\u30fb\u30fb\u30fb\/UserInfo&#8221;\u3068\u3044\u3046URL\u3092\u6301\u3064Web API\u304c\u4f5c\u6210\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UserInfoController.cs\n\nnamespace WebAPITest.Controllers\n{\n    [ApiController] \/\/ API\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067\u3042\u308b\u3053\u3068\u3092\u5ba3\u8a00\n    \/\/ \u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u8a2d\u5b9a\n    \/\/ \u3053\u306e\u4f8b\u3067\u306f\u3001http:\/\/\u30fb\u30fb\u30fb\/UserInfo [controller]\u306f\u30af\u30e9\u30b9\u540d\u304b\u3089\n    \/\/ Contrller\u3092\u53d6\u308a\u9664\u3044\u305f\u3082\u306e\u306b\u7f6e\u63db\u3055\u308c\u308b\n    \/\/ \u4eee\u60f3\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u968e\u5c64\u3092\u5909\u3048\u308b\u4e8b\u3082\u53ef\u80fd\u3002\u4f8b\u3048\u3070\u3001api\u306e\u4e0b\n    \/\/ (http:\/\/\u30fb\u30fb\u30fb\/api\/UserInfo)\u3068\u3057\u305f\u3051\u308c\u3070\u3001\n    \/\/ [Route(\"api\/[controller]\")]\n    \/\/ \u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u540d\u3068\u5168\u304f\u7570\u306a\u308bURL\u306b\u3059\u308b\u4e8b\u3082\u53ef\u80fd\n    [Route(\"[controller]\")]\n    public class UserInfoController : ControllerBase\n    {\n        \/\/ \u30d1\u30e9\u30e1\u30fc\u30bf\u7121\u3057GET\u306e\u51e6\u7406\n        \/\/ http:\/\/\u30fb\u30fb\u30fb\/UserInfo\/\n        [HttpGet]\n        public List&lt;UserInfoItem&gt; GetUserList()\n        {\n            \/\/ \u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\n            var lst = \u30fb\u30fb\u30fb.ToList();\n            return lst; \/\/ <span style=\"color:red;font-weight:bold\">\u81ea\u52d5\u7684\u306bJSON\u306b\u5909\u63db\u3055\u308c\u308b<\/span>\n        }\n        \/\/ \u30d1\u30e9\u30e1\u30fc\u30bfid\u3042\u308a\u306e\u51e6\u7406\u3002\n        \/\/ \u4f8b GET http:\/\/\u30fb\u30fb\u30fb\/UserInfo\/5\n        [HttpGet(\"{id}\")]\n        public UserInfoItem GetUserInfo(int id)\n        {\n              \/\/ \u6307\u5b9a\u3055\u308c\u305fID\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3092\u53d6\u5f97\n              var u = \u30fb\u30fb\u30fb\u30fb.FirstOrDefault();\n              if (u == null) {\n                  \/\/ ControllerBase\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3002\n                  \/\/ HTTP\u30d8\u30c3\u30c0 Status 404: Not Found\u3092\u8fd4\u3059\n                return NotFound();\n              } else {\n                  return u; \/\/ <span style=\"color:red;font-weight:bold\">\u81ea\u52d5\u7684\u306bJSON\u306b\u5909\u63db\u3055\u308c\u308b<\/span>\n              }\n          }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306fGET\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u307f\u3060\u304c\u3001POST\u3084PUT,DELETE\u3082\u5b9f\u88c5\u53ef\u80fd\u3002<\/p>\n\n\n\n<p>\u4f8b\u3048\u3070\u3001<em>[HttpDelete(\u201c{id}\u201d)]<\/em>\u306e\u3088\u3046\u306a\u5c5e\u6027\u3092\u4ed8\u3051\u308b\u3068\u3001<\/p>\n\n\n\n<p><em>DELETE \/UserInfo\/5<\/em><\/p>\n\n\n\n<p>\u306e\u3088\u3046\u306aURL\u3092\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3068\u306a\u308b\u3002<br>(\u901a\u5e38\u306f\u524a\u9664\u8981\u6c42\u51e6\u7406\u3092\u5b9f\u88c5\u3060\u3088\u306d)<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u306e\u3088\u3046\u306b\u304b\u306a\u308a\u7c21\u5358\u306bRESTful Web API\u304c\u5b9f\u88c5\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<p>\u8a73\u3057\u304f\u306f\u4e0b\u8a18URL\u3092\u53c2\u7167<br> <a href=\"https:\/\/docs.microsoft.com\/ja-jp\/aspnet\/core\/web-api\/?view=aspnetcore-3.1\">https:\/\/docs.microsoft.com\/ja-jp\/aspnet\/core\/web-api\/?view=aspnetcore-3.1<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET Core\u306b\u306fRESTful Web API\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u30c6\u30f3\u30d7 &hellip; <a href=\"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=155\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,9,11,4],"tags":[],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-c","category-dotnetcore","category-4"],"_links":{"self":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155"}],"version-history":[{"count":5,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions\/161"}],"wp:attachment":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}