{"id":1114,"date":"2025-05-27T14:46:30","date_gmt":"2025-05-27T06:46:30","guid":{"rendered":"https:\/\/www.hyy.net\/?p=1114"},"modified":"2025-05-27T14:46:30","modified_gmt":"2025-05-27T06:46:30","slug":"ultimate-asp-net-core-web-api-8-method-safety-and-method-idempotency","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=1114","title":{"rendered":"Ultimate ASP.NET Core Web API  8 METHOD SAFETY AND METHOD IDEMPOTENCY"},"content":{"rendered":"<p>8  METHOD SAFETY AND METHOD IDEMPOTENCY<br \/>\n8 \u65b9\u6cd5\u5b89\u5168\u548c\u65b9\u6cd5\u5e42\u7b49\u6027<\/p>\n<p>Before we start with the Create, Update, and Delete actions, we should explain two important principles in the HTTP standard. Those standards are Method Safety and Method Idempotency.\u200c<br \/>\n\u5728\u5f00\u59cb\u6267\u884c Create\u3001Update \u548c Delete\u4f5c\u4e4b\u524d\uff0c\u6211\u4eec\u5e94\u8be5\u89e3\u91ca HTTP \u6807\u51c6\u4e2d\u7684\u4e24\u4e2a\u91cd\u8981\u539f\u5219\u3002\u8fd9\u4e9b\u6807\u51c6\u662f\u65b9\u6cd5\u5b89\u5168\u548c\u65b9\u6cd5\u5e42\u7b49\u6027\u3002<\/p>\n<p>We can consider a method a safe one if it doesn\u2019t change the resource representation. So, in other words, the resource shouldn\u2019t be changed after our method is executed.<br \/>\n\u5982\u679c\u4e00\u4e2a\u65b9\u6cd5\u4e0d\u6539\u53d8\u8d44\u6e90\u8868\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba4\u4e3a\u5b83\u662f\u5b89\u5168\u7684\u3002\u6240\u4ee5\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u6211\u4eec\u7684\u65b9\u6cd5\u6267\u884c\u540e\uff0c\u8d44\u6e90\u4e0d\u5e94\u8be5\u88ab\u6539\u53d8\u3002<\/p>\n<p>If we can call a method multiple times with the same result, we can consider that method idempotent. So in other words, the side effects of calling it once are the same as calling it multiple times.<br \/>\n\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u4e00\u4e2a\u65b9\u6cd5\u5e76\u83b7\u5f97\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u8be5\u65b9\u6cd5\u662f\u5e42\u7b49\u7684\u3002\u6240\u4ee5\u6362\u53e5\u8bdd\u8bf4\uff0c\u8c03\u7528\u4e00\u6b21\u7684\u526f\u4f5c\u7528\u4e0e\u591a\u6b21\u8c03\u7528\u5b83\u7684\u526f\u4f5c\u7528\u76f8\u540c\u3002<\/p>\n<p>Let\u2019s see how this applies to HTTP methods:<br \/>\n\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u5982\u4f55\u5e94\u7528\u4e8e HTTP \u65b9\u6cd5\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>HTTP Method<\/th>\n<th>Is it Safe?<\/th>\n<th>Is it Idempotent?<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>GET<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>OPTIONS<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>HEAD<\/td>\n<td>Yes<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>POST<\/td>\n<td>No<\/td>\n<td>No<\/td>\n<\/tr>\n<tr>\n<td>DELETE<\/td>\n<td>No<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>PUT<\/td>\n<td>No<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>PATCH<\/td>\n<td>No<\/td>\n<td>No<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, the GET, OPTIONS, and HEAD methods are both safe and idempotent, because when we call those methods they will not change the resource representation. Furthermore, we can call these methods multiple times, but they will return the same result every time.<br \/>\n\u5982\u60a8\u6240\u89c1\uff0cGET\u3001OPTIONS \u548c HEAD \u65b9\u6cd5\u65e2\u5b89\u5168\u53c8\u5e42\u7b49\uff0c\u56e0\u4e3a\u5f53\u6211\u4eec\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\uff0c\u5b83\u4eec\u4e0d\u4f1a\u66f4\u6539\u8d44\u6e90\u8868\u793a\u5f62\u5f0f\u3002\u6b64\u5916\uff0c\u6211\u4eec\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f46\u5b83\u4eec\u6bcf\u6b21\u90fd\u4f1a\u8fd4\u56de\u76f8\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<p>The POST method is neither safe nor idempotent. It causes changes in the resource representation because it creates them. Also, if we call the POST method multiple times, it will create a new resource every time.<br \/>\nPOST \u65b9\u6cd5\u65e2\u4e0d\u5b89\u5168\u4e5f\u4e0d\u5e42\u7b49\u3002\u5b83\u4f1a\u5bfc\u81f4\u8d44\u6e90\u8868\u793a\u5f62\u5f0f\u53d1\u751f\u53d8\u5316\uff0c\u56e0\u4e3a\u5b83\u4f1a\u521b\u5efa\u8d44\u6e90\u8868\u793a\u5f62\u5f0f\u3002\u6b64\u5916\uff0c\u5982\u679c\u6211\u4eec\u591a\u6b21\u8c03\u7528 POST \u65b9\u6cd5\uff0c\u5b83\u6bcf\u6b21\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u8d44\u6e90\u3002<\/p>\n<p>The DELETE method is not safe because it removes the resource, but it is idempotent because if we delete the same resource multiple times, we will get the same result as if we have deleted it only once.<br \/>\nDELETE \u65b9\u6cd5\u4e0d\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5220\u9664\u8d44\u6e90\uff0c\u4f46\u5b83\u662f\u5e42\u7b49\u7684\uff0c\u56e0\u4e3a\u5982\u679c\u6211\u4eec\u591a\u6b21\u5220\u9664\u540c\u4e00\u8d44\u6e90\uff0c\u6211\u4eec\u5c06\u5f97\u5230\u4e0e\u53ea\u5220\u9664\u4e00\u6b21\u76f8\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<p>PUT is not safe either. When we update our resource, it changes. But it is idempotent because no matter how many times we update the same resource with the same request it will have the same representation as if we have updated it only once.<br \/>\nPUT \u4e5f\u4e0d\u5b89\u5168\u3002\u5f53\u6211\u4eec\u66f4\u65b0\u8d44\u6e90\u65f6\uff0c\u5b83\u4f1a\u53d1\u751f\u53d8\u5316\u3002\u4f46\u5b83\u662f\u5e42\u7b49\u7684\uff0c\u56e0\u4e3a\u65e0\u8bba\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u8bf7\u6c42\u66f4\u65b0\u540c\u4e00\u8d44\u6e90\u591a\u5c11\u6b21\uff0c\u5b83\u90fd\u5c06\u5177\u6709\u76f8\u540c\u7684\u8868\u793a\u5f62\u5f0f\uff0c\u5c31\u597d\u50cf\u6211\u4eec\u53ea\u66f4\u65b0\u4e86\u4e00\u6b21\u4e00\u6837\u3002<\/p>\n<p>Finally, the PATCH method is neither safe nor idempotent.<br \/>\n\u6700\u540e\uff0cPATCH \u65b9\u6cd5\u65e2\u4e0d\u5b89\u5168\u4e5f\u4e0d\u5e42\u7b49\u3002<\/p>\n<p>Now that we\u2019ve learned about these principles, we can continue with our application by implementing the rest of the HTTP methods (we have already implemented GET). We can always use this table to decide which method to use for which use case.<br \/>\n\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u8fd9\u4e9b\u539f\u5219\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0\u5176\u4f59\u7684 HTTP \u65b9\u6cd5\uff08\u6211\u4eec\u5df2\u7ecf\u5b9e\u73b0\u4e86 GET\uff09\u6765\u7ee7\u7eed\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u6211\u4eec\u59cb\u7ec8\u53ef\u4ee5\u4f7f\u7528\u6b64\u8868\u6765\u51b3\u5b9a\u5c06\u54ea\u79cd\u65b9\u6cd5\u7528\u4e8e\u54ea\u4e2a\u7528\u4f8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>8 METHOD SAFETY AND METHOD IDEMPOTENCY 8 \u65b9\u6cd5\u5b89\u5168\u548c\u65b9\u6cd5\u5e42\u7b49\u6027 Before we start with the Create, Update, and Delete actions, we should explain two important principles in the HTTP standard. Those standards are Method Safety and Method Idempotency.\u200c \u5728\u5f00\u59cb\u6267\u884c Create\u3001Update \u548c Delete\u4f5c\u4e4b\u524d\uff0c\u6211\u4eec\u5e94\u8be5\u89e3\u91ca HTTP \u6807\u51c6\u4e2d\u7684\u4e24\u4e2a\u91cd\u8981\u539f\u5219\u3002\u8fd9\u4e9b\u6807\u51c6\u662f\u65b9\u6cd5\u5b89\u5168\u548c\u65b9\u6cd5\u5e42\u7b49\u6027\u3002 We can consider a method a safe one if it doesn\u2019t change the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1114","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/1114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1114"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/1114\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}