{"id":3621,"date":"2025-04-20T19:56:56","date_gmt":"2025-04-20T11:56:56","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3621"},"modified":"2025-04-20T20:26:49","modified_gmt":"2025-04-20T12:26:49","slug":"%e4%bd%a0%e5%9c%a8%e9%a1%b9%e7%9b%ae%e4%b8%ad%e6%80%8e%e4%b9%88%e5%90%91%e5%89%8d%e7%ab%af%e4%bc%a0%e6%95%b0%e6%8d%ae%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/20\/%e4%bd%a0%e5%9c%a8%e9%a1%b9%e7%9b%ae%e4%b8%ad%e6%80%8e%e4%b9%88%e5%90%91%e5%89%8d%e7%ab%af%e4%bc%a0%e6%95%b0%e6%8d%ae%e7%9a%84%ef%bc%9f\/","title":{"rendered":"\u4f60\u5728\u9879\u76ee\u4e2d\u600e\u4e48\u5411\u524d\u7aef\u4f20\u6570\u636e\u7684\uff1f"},"content":{"rendered":"\n<p>\u8fd9\u662f\u9762\u8bd5\u7814\u53d1\u5c97\u4f4d\u65f6<strong>\u7ecf\u5e38\u95ee\u5230\u7684 Web \u6280\u672f\u7ec6\u8282\u95ee\u9898\u4e4b\u4e00<\/strong>\uff0c\u672c\u8d28\u4e0a\u8003\u5bdf\u4f60\u5bf9\u540e\u7aef\u5411\u524d\u7aef\u4f20\u8f93\u6570\u636e\u7684<strong>\u65b9\u5f0f\u3001\u683c\u5f0f\u3001\u534f\u8bae\u3001\u5b89\u5168\u3001\u6027\u80fd\u4f18\u5316<\/strong>\u7b49\u65b9\u9762\u7684\u7406\u89e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5e38\u89c1\u56de\u7b54\u601d\u8def\uff08\u5feb\u901f\u7248\uff09<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u201c\u6211\u4eec\u9879\u76ee\u662f\u524d\u540e\u7aef\u5206\u79bb\u67b6\u6784\uff0c\u540e\u7aef\u91c7\u7528 RESTful API \u6216 GraphQL\uff0c\u5c06\u6570\u636e\u4ee5 JSON \u683c\u5f0f\u901a\u8fc7 HTTP\/HTTPS \u534f\u8bae\u8fd4\u56de\u524d\u7aef\u3002\u5177\u4f53\u7528\u5230 .NET Core Web API \u6846\u67b6\uff0c\u4f7f\u7528\u5185\u7f6e\u7684 <code>JsonSerializer<\/code> \u8fdb\u884c\u6570\u636e\u5e8f\u5217\u5316\uff0c\u54cd\u5e94\u7edf\u4e00\u5c01\u88c5\u4e3a\u6807\u51c6\u683c\u5f0f\uff08\u5982 code\u3001message\u3001data\uff09\u3002\u5fc5\u8981\u65f6\u652f\u6301\u5206\u9875\u3001\u8fc7\u6ee4\u3001\u6392\u5e8f\u7b49\u67e5\u8be2\u53c2\u6570\u3002\u201d<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5b8c\u6574\u6280\u672f\u8981\u70b9\u8be6\u89e3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6570\u636e\u4f20\u8f93\u534f\u8bae\uff08HTTP\/HTTPS\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u540e\u7aef\u63a5\u53e3\u901a\u8fc7 <strong>HTTP \u6216 HTTPS<\/strong> \u534f\u8bae\u5411\u524d\u7aef\u4f20\u8f93\u6570\u636e<\/li>\n\n\n\n<li>\u901a\u5e38\u4f7f\u7528 RESTful \u98ce\u683c\uff08GET\u3001POST\u3001PUT\u3001DELETE\uff09\u6216 GraphQL<\/li>\n\n\n\n<li>\u82e5\u4f7f\u7528 SignalR\uff0c\u5219\u662f\u57fa\u4e8e WebSocket \u7684\u5b9e\u65f6\u6570\u636e\u63a8\u9001<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6570\u636e\u683c\u5f0f\uff08JSON \/ XML\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9ed8\u8ba4\u4f7f\u7528 <strong>JSON<\/strong> \u683c\u5f0f\uff08\u8f7b\u91cf\u3001\u6613\u89e3\u6790\u3001\u6613\u4e0e JS \u5bf9\u63a5\uff09<\/li>\n\n\n\n<li>\u4e5f\u53ef\u4f7f\u7528 XML\u3001\u4e8c\u8fdb\u5236\uff08\u5982 Protocol Buffers\uff0c\u4f46\u524d\u7aef\u652f\u6301\u5f31\uff09<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"code\": 200,\n  \"message\": \"success\",\n  \"data\": {\n    \"userId\": 123,\n    \"userName\": \"\u5f20\u4e09\"\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u6570\u636e\u5e8f\u5217\u5316<\/h3>\n\n\n\n<p>\u5728 C# \u4e2d\uff0c\u5e38\u7528\u7684\u5e8f\u5217\u5316\u5e93\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5e8f\u5217\u5316\u5de5\u5177<\/th><th>\u7279\u70b9<\/th><\/tr><\/thead><tbody><tr><td><code>System.Text.Json<\/code><\/td><td>.NET Core \u539f\u751f\u652f\u6301\uff0c\u6027\u80fd\u4f18<\/td><\/tr><tr><td><code>Newtonsoft.Json<\/code><\/td><td>\u529f\u80fd\u5f3a\u5927\uff0c\u7075\u6d3b<\/td><\/tr><tr><td><code>protobuf-net<\/code><\/td><td>\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\uff0c\u7528\u4e8e\u9ad8\u6027\u80fd\u573a\u666f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>return Ok(new {\n    code = 200,\n    message = \"success\",\n    data = new { name = \"Alice\", age = 28 }\n});<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u524d\u540e\u7aef\u5e38\u89c1\u4ea4\u4e92\u65b9\u5f0f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u63a5\u53e3\u7c7b\u578b<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>RESTful API<\/td><td>\u4e3b\u6d41\u65b9\u5f0f\uff0c\u57fa\u4e8e HTTP \u65b9\u6cd5<\/td><\/tr><tr><td>GraphQL<\/td><td>\u6309\u9700\u67e5\u8be2\u5b57\u6bb5\uff0c\u524d\u7aef\u63a7\u5236\u7c92\u5ea6<\/td><\/tr><tr><td>SignalR \/ WebSocket<\/td><td>\u5b9e\u65f6\u901a\u4fe1\uff0c\u6570\u636e\u63a8\u9001<\/td><\/tr><tr><td>gRPC<\/td><td>\u9ad8\u6027\u80fd\u4e8c\u8fdb\u5236\u901a\u4fe1\uff0c\u4e00\u822c\u7528\u4e8e\u5185\u90e8\u670d\u52a1\u8c03\u7528<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u9879\u76ee\u4e2d\u5e38\u7528\u7684\u5c01\u88c5\u4e0e\u5b9e\u8df5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u7edf\u4e00\u54cd\u5e94\u683c\u5f0f<\/h3>\n\n\n\n<p>\u5c01\u88c5\u7edf\u4e00\u7684 <code>ApiResult&lt;T&gt;<\/code> \u54cd\u5e94\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ApiResponse&lt;T&gt; {\n    public int Code { get; set; }\n    public string Message { get; set; }\n    public T Data { get; set; }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u53c2\u6570\u63a5\u6536 + \u6570\u636e\u8f6c\u6362<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63a7\u5236\u5668\u63a5\u6536\u53c2\u6570 \u2192 \u8c03\u7528\u4e1a\u52a1\u670d\u52a1 \u2192 \u6570\u636e\u6620\u5c04\uff08AutoMapper\uff09\u2192 \u8fd4\u56de DTO<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;HttpGet(\"{id}\")]\npublic async Task&lt;IActionResult&gt; GetUser(int id) {\n    var user = await _userService.GetUserByIdAsync(id);\n    var dto = _mapper.Map&lt;UserDto&gt;(user);\n    return Ok(ApiResponse.Success(dto));\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u5206\u9875\u4e0e\u590d\u6742\u67e5\u8be2\u53c2\u6570<\/h2>\n\n\n\n<p>\u9879\u76ee\u4e2d\u7ecf\u5e38\u9700\u8981\u4f20\u8f93\u5206\u9875\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET \/api\/users?page=1&amp;pageSize=20&amp;search=Tom<\/code><\/pre>\n\n\n\n<p>\u540e\u7aef\u4f7f\u7528 <code>[FromQuery]<\/code> \u7ed1\u5b9a\u5206\u9875\u67e5\u8be2\u5bf9\u8c61\uff0c\u7edf\u4e00\u8fd4\u56de\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"code\": 200,\n  \"message\": \"success\",\n  \"data\": {\n    \"total\": 100,\n    \"items\": &#91; ... ]\n  }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5411\u524d\u7aef\u4f20\u6570\u636e\u7684\u9644\u52a0\u70b9\uff08\u53ef\u4ee5\u52a0\u5206\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6280\u672f\u70b9<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>\u2705 CORS \u8de8\u57df\u652f\u6301<\/td><td>\u524d\u540e\u7aef\u5206\u79bb\u90e8\u7f72\u5fc5\u5907<\/td><\/tr><tr><td>\u2705 JWT \/ Cookie \u8ba4\u8bc1\u643a\u5e26\u6570\u636e<\/td><td>\u7528\u6237\u8eab\u4efd\u8bc6\u522b<\/td><\/tr><tr><td>\u2705 \u6570\u636e\u6743\u9650\u63a7\u5236<\/td><td>\u8fd4\u56de\u524d\u8fc7\u6ee4\u7528\u6237\u6709\u6743\u8bbf\u95ee\u7684\u6570\u636e<\/td><\/tr><tr><td>\u2705 \u538b\u7f29\u54cd\u5e94\uff08Gzip\/Brotli\uff09<\/td><td>\u63d0\u5347\u4f20\u8f93\u6027\u80fd<\/td><\/tr><tr><td>\u2705 DTO \u5c42\u5206\u79bb<\/td><td>\u907f\u514d Entity \u76f4\u63a5\u66b4\u9732\uff0c\u63d0\u9ad8\u5b89\u5168\u6027<\/td><\/tr><tr><td>\u2705 \u7f13\u5b58\u6570\u636e<\/td><td>\u70ed\u95e8\u6570\u636e\u7f13\u5b58\u540e\u8fd4\u56de\u524d\u7aef\uff0c\u63d0\u5347\u6027\u80fd<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u9762\u8bd5\u7b54\u9898\u6a21\u677f\uff08\u8303\u4f8b\uff09<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u201c\u6211\u4eec\u9879\u76ee\u91c7\u7528\u524d\u540e\u7aef\u5206\u79bb\u67b6\u6784\uff0c\u540e\u7aef\u662f ASP.NET Core Web API\uff0c\u5411\u524d\u7aef\u901a\u8fc7 RESTful \u63a5\u53e3\u8fd4\u56de JSON \u683c\u5f0f\u7684\u6570\u636e\u3002\u6570\u636e\u5728 Controller \u4e2d\u5904\u7406\u540e\u4f7f\u7528 AutoMapper \u6620\u5c04\u6210 DTO\uff0c\u901a\u8fc7\u5c01\u88c5\u597d\u7684\u7edf\u4e00\u8fd4\u56de\u7ed3\u6784\u8fd4\u56de\u3002\u5206\u9875\u548c\u67e5\u8be2\u652f\u6301\u52a8\u6001\u53c2\u6570\uff0c\u7edf\u4e00\u54cd\u5e94 total \u548c items\u3002\u90e8\u5206\u9ad8\u9891\u63a5\u53e3\u8fd8\u505a\u4e86\u7f13\u5b58\u4f18\u5316\u3002\u5b89\u5168\u65b9\u9762\u4f7f\u7528 JWT \u8ba4\u8bc1\uff0c\u5e76\u542f\u7528\u4e86 CORS \u7b56\u7565\u4ee5\u652f\u6301\u8de8\u57df\u8bf7\u6c42\u3002\u201d<\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u9762\u8bd5\u7814\u53d1\u5c97\u4f4d\u65f6\u7ecf\u5e38\u95ee\u5230\u7684 Web \u6280\u672f\u7ec6\u8282\u95ee\u9898\u4e4b\u4e00\uff0c\u672c\u8d28\u4e0a\u8003\u5bdf\u4f60\u5bf9\u540e\u7aef\u5411\u524d\u7aef\u4f20\u8f93\u6570\u636e\u7684\u65b9\u5f0f\u3001\u683c\u5f0f\u3001\u534f\u8bae\u3001\u5b89 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3622,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85],"tags":[],"class_list":["post-3621","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-redis"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3621"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3621\/revisions"}],"predecessor-version":[{"id":3623,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3621\/revisions\/3623"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3622"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}