{"id":1213,"date":"2025-01-25T23:05:10","date_gmt":"2025-01-25T15:05:10","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1213"},"modified":"2025-01-25T23:05:11","modified_gmt":"2025-01-25T15:05:11","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e4%b8%ad%e5%ae%89%e5%85%a8%e5%9c%b0%e7%bc%96%e7%a0%81%e7%94%a8%e6%88%b7%e5%90%8d%e5%b9%b6%e5%88%9b%e5%bb%ba-url-%e8%b7%af%e5%be%84%ef%bc%9a%e5%ae%8c%e6%95%b4%e6%8c%87","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e4%b8%ad%e5%ae%89%e5%85%a8%e5%9c%b0%e7%bc%96%e7%a0%81%e7%94%a8%e6%88%b7%e5%90%8d%e5%b9%b6%e5%88%9b%e5%bb%ba-url-%e8%b7%af%e5%be%84%ef%bc%9a%e5%ae%8c%e6%95%b4%e6%8c%87\/","title":{"rendered":"\u5982\u4f55\u5728 C# \u4e2d\u5b89\u5168\u5730\u7f16\u7801\u7528\u6237\u540d\u5e76\u521b\u5efa URL \u8def\u5f84\uff1a\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u672c\u6587\u63d0\u4f9b\u4e86\u5173\u4e8e\u5982\u4f55\u5728 C# \u4e2d\u5b89\u5168\u5730\u7f16\u7801\u7528\u6237\u540d\u5e76\u521b\u5efa URL \u8def\u5f84\u7684\u5b8c\u6574\u6307\u5357\u3002\u65e0\u8bba\u4f60\u662f\u4f7f\u7528\u00a0<code>Uri.EscapeDataString<\/code>\u3001<code>HttpUtility.UrlEncode<\/code>\u00a0\u8fd8\u662f\u81ea\u5b9a\u4e49\u7f16\u7801\u65b9\u6cd5\uff0c\u672c\u6559\u7a0b\u5c06\u5e2e\u52a9\u4f60\u907f\u514d\u6f5c\u5728\u7684\u5b89\u5168\u95ee\u9898\u5e76\u786e\u4fdd\u751f\u6210\u7684 URL \u8def\u5f84\u7b26\u5408\u6807\u51c6\u3002\u5b66\u4e60\u8fd9\u4e9b\u5b9e\u7528\u7684 C# \u7f16\u7801\u6280\u5de7\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 URL \u5b89\u5168\u3001\u6709\u6548\uff0c\u5e76\u80fd\u591f\u6b63\u786e\u5904\u7406\u7528\u6237\u8f93\u5165\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528 <code>Uri.EscapeDataString<\/code> \u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C# \u63d0\u4f9b\u4e86 <code>Uri.EscapeDataString<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u975e ASCII \u5b57\u7b26\u3001\u7279\u6b8a\u5b57\u7b26\u7b49\u8f6c\u6362\u4e3a URL \u7f16\u7801\u683c\u5f0f\u3002\u5b83\u975e\u5e38\u9002\u7528\u4e8e\u7f16\u7801 URL \u8def\u5f84\u4e2d\u7684\u90e8\u5206\uff08\u5982\u7528\u6237\u540d\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nclass Program\n{\n    static void Main()\n    {\n        string username = \"john_doe@website.com\";\n        string encodedUsername = Uri.EscapeDataString(username);\n        Console.WriteLine(encodedUsername);  \/\/ \u8f93\u51fa john_doe%40website.com\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Uri.EscapeDataString<\/code> \u4f1a\u5c06\u7528\u6237\u540d\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff08\u5982 <code>@<\/code>\uff09\u8f6c\u6362\u4e3a URL \u7f16\u7801\uff08\u5982 <code>%40<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7f16\u7801 URL \u7684\u8def\u5f84\u90e8\u5206\uff0c\u4f46\u5b83\u4e0d\u4f1a\u5904\u7406\u5168\u5c40\u7684 URL \u7f16\u7801\uff08\u5982\u659c\u6760 <code>\/<\/code> \u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>HttpUtility.UrlEncode<\/code> \u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HttpUtility.UrlEncode<\/code> \u65b9\u6cd5\u5c5e\u4e8e <code>System.Web<\/code> \u547d\u540d\u7a7a\u95f4\uff0c\u5b83\u7528\u4e8e\u5bf9\u6574\u4e2a URL \u8fdb\u884c\u7f16\u7801\u3002\u4e0e <code>Uri.EscapeDataString<\/code> \u7c7b\u4f3c\uff0c\u5b83\u5c06\u7279\u6b8a\u5b57\u7b26\u8f6c\u6362\u4e3a\u53ef\u5728 URL \u4e2d\u5b89\u5168\u4f20\u8f93\u7684\u683c\u5f0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Web;\n\nclass Program\n{\n    static void Main()\n    {\n        string username = \"john_doe@website.com\";\n        string encodedUsername = HttpUtility.UrlEncode(username);\n        Console.WriteLine(encodedUsername);  \/\/ \u8f93\u51fa john_doe%40website.com\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>HttpUtility.UrlEncode<\/code> \u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u8fdb\u884c\u7f16\u7801\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u7528\u6237\u8f93\u5165\u7684 URL \u53c2\u6570\u3002<\/li>\n\n\n\n<li>\u6ce8\u610f\uff1a<code>HttpUtility<\/code> \u662f\u5728 <code>System.Web<\/code> \u4e2d\uff0c\u56e0\u6b64\u9700\u8981\u5f15\u7528\u8be5\u547d\u540d\u7a7a\u95f4\u3002\u5bf9\u4e8e ASP.NET \u9879\u76ee\uff0c\u8fd9\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u505a\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528 <code>WebUtility.UrlEncode<\/code> \u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4e0d\u60f3\u5f15\u7528 <code>System.Web<\/code> \u547d\u540d\u7a7a\u95f4\uff0c<code>System.Net.WebUtility.UrlEncode<\/code> \u63d0\u4f9b\u4e86\u76f8\u4f3c\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u8f83\u65b0\u7684 .NET Core \u548c .NET 5+ \u9879\u76ee\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Net;\n\nclass Program\n{\n    static void Main()\n    {\n        string username = \"john_doe@website.com\";\n        string encodedUsername = WebUtility.UrlEncode(username);\n        Console.WriteLine(encodedUsername);  \/\/ \u8f93\u51fa john_doe%40website.com\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>WebUtility.UrlEncode<\/code> \u7684\u529f\u80fd\u4e0e <code>HttpUtility.UrlEncode<\/code> \u7c7b\u4f3c\uff0c\u53ef\u4ee5\u5bf9 URL \u4e2d\u7684\u7528\u6237\u540d\u7b49\u8fdb\u884c\u7f16\u7801\u3002<\/li>\n\n\n\n<li>\u5b83\u662f <code>System.Net<\/code> \u547d\u540d\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u7528\u4e8e\u73b0\u4ee3\u7684 .NET \u6838\u5fc3\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u81ea\u5b9a\u4e49\u7f16\u7801\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u7f16\u7801\u89c4\u5219\uff0c\u6216\u8005\u5728\u4e0d\u4f7f\u7528\u5e93\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\u624b\u52a8\u8fdb\u884c\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u5c06\u7279\u5b9a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5176 URL \u7f16\u7801\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Text;\n\nclass Program\n{\n    static string EncodeUsername(string username)\n    {\n        StringBuilder encoded = new StringBuilder();\n        foreach (char c in username)\n        {\n            if (Char.IsLetterOrDigit(c) || c == '_' || c == '-')\n            {\n                encoded.Append(c);  \/\/ \u5141\u8bb8\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\u548c\u7834\u6298\u53f7\n            }\n            else\n            {\n                encoded.Append('%').Append(((int)c).ToString(\"X2\"));  \/\/ \u7f16\u7801\u5176\u4ed6\u5b57\u7b26\n            }\n        }\n        return encoded.ToString();\n    }\n\n    static void Main()\n    {\n        string username = \"john_doe@website.com\";\n        string encodedUsername = EncodeUsername(username);\n        Console.WriteLine(encodedUsername);  \/\/ \u8f93\u51fa john_doe%40website.com\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u5b9a\u4e49\u7f16\u7801\u65b9\u6cd5\u4ec5\u5bf9\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\u548c\u7834\u6298\u53f7\u5b57\u7b26\u8fdb\u884c\u539f\u6837\u5904\u7406\uff0c\u5176\u4ed6\u5b57\u7b26\u5219\u4f7f\u7528 <code>%XX<\/code> \u7f16\u7801\u683c\u5f0f\uff08\u5176\u4e2d <code>XX<\/code> \u662f\u5b57\u7b26\u7684 ASCII \u5341\u516d\u8fdb\u5236\u503c\uff09\u3002<\/li>\n\n\n\n<li>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7279\u6b8a\u7684\u7f16\u7801\u9700\u6c42\uff0c\u4f46\u4e0d\u5982\u5185\u7f6e\u65b9\u6cd5\u5168\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u79fb\u9664\u975e\u6cd5\u5b57\u7b26<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u7f16\u7801\u5916\uff0c\u6709\u65f6\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u79fb\u9664\u4e0d\u5408\u6cd5\u7684\u5b57\u7b26\uff08\u4f8b\u5982\uff0c\u7a7a\u683c\u3001\u67d0\u4e9b\u6807\u70b9\u7b26\u53f7\u7b49\uff09\u3002\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u7406\u7528\u6237\u540d\uff0c\u4f7f\u5176\u7b26\u5408 URL \u7684\u89c4\u5b9a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Text.RegularExpressions;\n\nclass Program\n{\n    static string CleanAndEncodeUsername(string username)\n    {\n        string cleanUsername = Regex.Replace(username, @\"&#91;^a-zA-Z0-9_-]\", \"_\");\n        return Uri.EscapeDataString(cleanUsername);\n    }\n\n    static void Main()\n    {\n        string username = \"john doe@website.com\";\n        string encodedUsername = CleanAndEncodeUsername(username);\n        Console.WriteLine(encodedUsername);  \/\/ \u8f93\u51fa john_doe%40website.com\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f <code>@\"[^a-zA-Z0-9_-]\"<\/code> \u4f1a\u5339\u914d\u6240\u6709\u4e0d\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\u548c\u7834\u6298\u53f7\u7684\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u4e0b\u5212\u7ebf <code>_<\/code>\u3002<\/li>\n\n\n\n<li>\u8fd9\u79cd\u65b9\u6cd5\u7ed3\u5408\u4e86\u6e05\u7406\u548c\u7f16\u7801\uff0c\u786e\u4fdd\u7528\u6237\u540d\u7b26\u5408 URL \u89c4\u8303\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6bd4\u8f83\u548c\u9009\u62e9\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Uri.EscapeDataString<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u90e8\u5206\u57fa\u672c\u7f16\u7801\u9700\u6c42\uff0c\u8f7b\u4fbf\u9ad8\u6548\u3002<\/li>\n\n\n\n<li><strong><code>HttpUtility.UrlEncode<\/code> \u548c <code>WebUtility.UrlEncode<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u8f83\u5b8c\u6574\u7684 URL \u7f16\u7801\u9700\u6c42\uff0c\u9002\u5408 ASP.NET \u548c .NET Core \u9879\u76ee\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u7f16\u7801\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u66f4\u7279\u5b9a\u7684\u7f16\u7801\u9700\u6c42\uff0c\u4f46\u76f8\u5bf9\u8f83\u590d\u6742\u3002<\/li>\n\n\n\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u7406<\/strong>\uff1a\u9002\u7528\u4e8e\u6e05\u7406\u548c\u66ff\u6362\u4e0d\u5408\u6cd5\u5b57\u7b26\uff0c\u786e\u4fdd\u7528\u6237\u540d\u7b26\u5408 URL \u89c4\u8303\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u63d0\u4f9b\u4e86\u5173\u4e8e\u5982\u4f55\u5728 C# \u4e2d\u5b89\u5168\u5730\u7f16\u7801\u7528\u6237\u540d\u5e76\u521b\u5efa URL \u8def\u5f84\u7684\u5b8c\u6574\u6307\u5357\u3002\u65e0\u8bba\u4f60\u662f\u4f7f\u7528\u00a0Uri.Escap [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-1213","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1213","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=1213"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1213\/revisions"}],"predecessor-version":[{"id":1214,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1213\/revisions\/1214"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}