{"id":10723,"date":"2025-11-15T13:51:49","date_gmt":"2025-11-15T05:51:49","guid":{"rendered":"https:\/\/as32.net\/blog\/?p=10717"},"modified":"2025-11-15T15:09:47","modified_gmt":"2025-11-15T07:09:47","slug":"%e5%9c%a8-net-miniapi-kestrel-%e4%b8%ad%e5%ae%9e%e7%8e%b0%e9%9d%99%e6%80%81%e6%96%87%e4%bb%b6%e6%9c%8d%e5%8a%a1-%e5%8f%af%e6%8e%92%e5%ba%8f%e7%9b%ae%e5%bd%95%e6%b5%8f%e8%a7%88%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/bohu.net\/blog\/10723\/","title":{"rendered":"\u5728 .NET MiniAPI (Kestrel) \u4e2d\u5b9e\u73b0\u9759\u6001\u6587\u4ef6\u670d\u52a1 + \u53ef\u6392\u5e8f\u76ee\u5f55\u6d4f\u89c8\u529f\u80fd"},"content":{"rendered":"\n<p>\u5728\u5f00\u53d1\u8f7b\u91cf\u7ea7 API \u65f6\uff0c.NET MiniAPI \u51ed\u501f\u5176\u7b80\u6d01\u9ad8\u6548\u7684\u7279\u6027\u6210\u4e3a\u9996\u9009\u3002\u4f46\u5b9e\u9645\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u5e38\u9700\u8981\u66b4\u9732\u9759\u6001\u6587\u4ef6\uff08\u5982 API \u6587\u6863\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u5171\u4eab\u8d44\u6e90\uff09\uff0c\u751a\u81f3\u5141\u8bb8\u5ba2\u6237\u7aef\u6d4f\u89c8\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u5217\u8868\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cMiniAPI \u7684\u9759\u6001\u6587\u4ef6\u670d\u52a1\u4e0d\u652f\u6301\u76ee\u5f55\u6d4f\u89c8\uff0c\u4e14\u539f\u751f\u76ee\u5f55\u5217\u8868\u65e0\u6392\u5e8f\u529f\u80fd\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u8bb0\u5f55\u5982\u4f55\u5b9e\u73b0\u300c\u6307\u5b9a\u76ee\u5f55\u66b4\u9732 + \u76ee\u5f55\u6d4f\u89c8 + \u5217\u8868\u6392\u5e8f\u300d\u7684\u5b8c\u6574\u65b9\u6848\uff0c\u9644\u5b8c\u6574\u4ee3\u7801\u548c\u4f18\u5316\u7ec6\u8282\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u9700\u6c42\u80cc\u666f<\/h2>\n\n\n\n<p>\u5728 MiniAPI \u4e2d\u5b9e\u73b0\u4ee5\u4e0b\u6838\u5fc3\u529f\u80fd\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u66b4\u9732\u670d\u52a1\u5668\u4e0a\u7684\u6307\u5b9a\u7269\u7406\u76ee\u5f55\uff08\u5982 <code>GeoIP<\/code> \u6587\u4ef6\u5939\uff09\uff0c\u652f\u6301\u5ba2\u6237\u7aef\u901a\u8fc7 URL \u8bbf\u95ee\u6587\u4ef6\uff1b<\/li>\n\n\n\n<li>\u542f\u7528\u76ee\u5f55\u6d4f\u89c8\u529f\u80fd\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u5217\u51fa\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\/\u5b50\u76ee\u5f55\uff1b<\/li>\n\n\n\n<li>\u76ee\u5f55\u5217\u8868\u652f\u6301\u6309\u300c\u540d\u79f0\u3001\u5927\u5c0f\u3001\u4fee\u6539\u65f6\u95f4\u300d\u6392\u5e8f\uff08\u70b9\u51fb\u8868\u5934\u5207\u6362\u5347\u5e8f\/\u964d\u5e8f\uff09\uff1b<\/li>\n\n\n\n<li>\u4f18\u5316\u76ee\u5f55\u5217\u8868\u6837\u5f0f\uff0c\u4fdd\u6301\u7b80\u6d01\u6613\u7528\uff0c\u540c\u65f6\u517c\u5bb9 AOT \u7f16\u8bd1\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5b9e\u73b0\u6b65\u9aa4\uff08\u57fa\u4e8e .NET  MiniAPI  &gt; 8.0\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u73af\u5883\u51c6\u5907<\/h3>\n\n\n\n<p>\u521b\u5efa .NET  MiniAPI \u9879\u76ee\uff08\u82e5\u5df2\u6709\u9879\u76ee\u53ef\u8df3\u8fc7\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dotnet new web -n MiniApiStaticFileDemo\ncd MiniApiStaticFileDemo<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6838\u5fc3\u914d\u7f6e\uff1a\u66b4\u9732\u6307\u5b9a\u7269\u7406\u76ee\u5f55 + \u542f\u7528\u76ee\u5f55\u6d4f\u89c8<\/h3>\n\n\n\n<p>\u5728 <code>Program.cs<\/code> \u4e2d\u914d\u7f6e\u9759\u6001\u6587\u4ef6\u4e2d\u95f4\u4ef6\u548c\u76ee\u5f55\u6d4f\u89c8\u529f\u80fd\uff0c\u6838\u5fc3\u662f\u6307\u5b9a\u7269\u7406\u76ee\u5f55\u3001URL \u8bbf\u95ee\u8def\u5f84\uff0c\u5e76\u5173\u8054\u81ea\u5b9a\u4e49\u6392\u5e8f\u683c\u5f0f\u5316\u5668\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 Program.cs \u5b8c\u6574\u914d\u7f6e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using Microsoft.AspNetCore.StaticFiles;\nusing Microsoft.Extensions.FileProviders;\nusing System.Text;\n\nvar builder = WebApplication.CreateSlimBuilder(args);\nvar app = builder.Build();\n\n\/\/ \u5173\u952e\u914d\u7f6e\uff1a\u6307\u5b9a\u8981\u66b4\u9732\u7684\u7269\u7406\u76ee\u5f55\nstring baseDir = AppContext.BaseDirectory; \/\/ \u7a0b\u5e8f\u8fd0\u884c\u76ee\u5f55\nstring exposeDir = Path.Combine(baseDir, \"GeoIP\"); \/\/ \u8981\u66b4\u9732\u7684\u76ee\u5f55\uff08\u5982 GeoIP \u6587\u4ef6\u5939\uff09\nstring urlPrefix = \"\/geoip\"; \/\/ \u5ba2\u6237\u7aef\u8bbf\u95ee\u524d\u7f00\uff08\u901a\u8fc7 \/geoip \u8bbf\u95ee\u8be5\u76ee\u5f55\uff09\n\n\/\/ 1. \u914d\u7f6e\u9759\u6001\u6587\u4ef6\u670d\u52a1\uff1a\u66b4\u9732\u6307\u5b9a\u7269\u7406\u76ee\u5f55\napp.UseStaticFiles(new StaticFileOptions\n{\n    FileProvider = new PhysicalFileProvider(exposeDir), \/\/ \u7269\u7406\u76ee\u5f55\u8def\u5f84\n    RequestPath = urlPrefix, \/\/ URL \u8bbf\u95ee\u8def\u5f84\uff08\u4f8b\uff1ahttp:\/\/localhost:5000\/geoip\/\u6587\u4ef6\u540d\u79f0\uff09\n    OnPrepareResponse = ctx =&gt;\n    {\n        \/\/ \u53ef\u9009\uff1a\u6dfb\u52a0\u54cd\u5e94\u5934\uff0c\u7981\u6b62\u7f13\u5b58\u9759\u6001\u6587\u4ef6\n        ctx.Context.Response.Headers.Append(\"Cache-Control\", \"no-cache, no-store\");\n    }\n});\n\n\/\/ 2. \u542f\u7528\u76ee\u5f55\u6d4f\u89c8\uff08\u9ed8\u8ba4\u7981\u7528\uff09\uff0c\u5e76\u5173\u8054\u81ea\u5b9a\u4e49\u6392\u5e8f\u683c\u5f0f\u5316\u5668\napp.UseDirectoryBrowser(new DirectoryBrowserOptions\n{\n    FileProvider = new PhysicalFileProvider(exposeDir),\n    RequestPath = urlPrefix, \/\/ \u4e0e\u9759\u6001\u6587\u4ef6\u8bbf\u95ee\u8def\u5f84\u4fdd\u6301\u4e00\u81f4\n    Formatter = new SortableDirectoryFormatter(\"GeoIP \u76ee\u5f55\u5217\u8868\") \/\/ \u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u5668\uff08\u652f\u6301\u6392\u5e8f\uff09\n});\n\n\/\/ \u6d4b\u8bd5\u63a5\u53e3\napp.MapGet(\"\/\", () =&gt; Results.Ok(\"MiniAPI \u9759\u6001\u6587\u4ef6\u670d\u52a1 + \u53ef\u6392\u5e8f\u76ee\u5f55\u6d4f\u89c8\u5df2\u542f\u7528\"));\n\napp.Run(\"http:\/\/*:5000\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5173\u952e\u5b9e\u73b0\uff1a\u81ea\u5b9a\u4e49\u53ef\u6392\u5e8f\u76ee\u5f55\u683c\u5f0f\u5316\u5668<\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u7684\u76ee\u5f55\u6d4f\u89c8\u5217\u8868\u65e0\u6392\u5e8f\u529f\u80fd\uff0c\u4e14\u6837\u5f0f\u7b80\u964b\u3002\u6211\u4eec\u901a\u8fc7\u5b9e\u73b0 <code>IDirectoryFormatter<\/code> \u63a5\u53e3\uff0c\u81ea\u5b9a\u4e49\u76ee\u5f55\u5217\u8868\u7684 HTML \u8f93\u51fa\uff0c\u6dfb\u52a0\u300c\u540d\u79f0\u3001\u5927\u5c0f\u3001\u4fee\u6539\u65f6\u95f4\u300d\u6392\u5e8f\u529f\u80fd\uff0c\u540c\u65f6\u4f18\u5316\u6837\u5f0f\u548c\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 \u5b8c\u6574 SortableDirectoryFormatter \u7c7b<\/h4>\n\n\n\n<p>\u521b\u5efa <code>SortableDirectoryFormatter.cs<\/code> \u6587\u4ef6\uff0c\u6838\u5fc3\u529f\u80fd\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fc7\u6ee4\u9690\u85cf\u6587\u4ef6\uff08\u4ee5 <code>.<\/code> \u5f00\u5934\u7684\u6587\u4ef6\uff09\uff1b<\/li>\n\n\n\n<li>\u76ee\u5f55\u5728\u524d\u3001\u6587\u4ef6\u5728\u540e\u7684\u9ed8\u8ba4\u6392\u5e8f\uff1b<\/li>\n\n\n\n<li>\u70b9\u51fb\u8868\u5934\u5207\u6362\u6392\u5e8f\u65b9\u5411\uff08\u5347\u5e8f\/\u964d\u5e8f\uff09\uff1b<\/li>\n\n\n\n<li>\u652f\u6301\u6309\u540d\u79f0\u3001\u6587\u4ef6\u5927\u5c0f\u3001\u4fee\u6539\u65f6\u95f4\u6392\u5e8f\uff1b<\/li>\n\n\n\n<li>XSS \u9632\u62a4\u3001\u8de8\u5e73\u53f0\u8def\u5f84\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>using Microsoft.AspNetCore.Http;\nusing Microsoft.Extensions.FileProviders;\nusing System.Globalization;\nusing System.Text;\n\npublic class SortableDirectoryFormatter : IDirectoryFormatter\n{\n    private readonly string _pageTitle;\n    \/\/ \u65f6\u95f4\u683c\u5f0f\uff1a\u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2\uff08UTC \u65f6\u533a\uff09\n    private const string CustomDateTimeFormat = \"yyyy-MM-dd HH:mm:ss +00:00\";\n\n    \/\/ \u6784\u9020\u51fd\u6570\uff1a\u652f\u6301\u81ea\u5b9a\u4e49\u9875\u9762\u6807\u9898\n    public SortableDirectoryFormatter(string pageTitle = \"\u76ee\u5f55\u5217\u8868\")\n    {\n        _pageTitle = pageTitle ?? \"\u76ee\u5f55\u5217\u8868\";\n    }\n\n    public async Task GenerateContentAsync(HttpContext context, IEnumerable&lt;IFileInfo&gt; contents)\n    {\n        \/\/ 1. \u8fc7\u6ee4\u9690\u85cf\u6587\u4ef6 + \u521d\u59cb\u6392\u5e8f\uff08\u76ee\u5f55\u5728\u524d\uff0c\u6587\u4ef6\u5728\u540e\uff09\n        var safeContents = contents ?? Enumerable.Empty&lt;IFileInfo&gt;();\n        var fileList = new List&lt;IFileInfo&gt;(safeContents.Where(f =&gt;\n            !string.IsNullOrEmpty(f.Name) &amp;&amp; !f.Name.StartsWith(\".\")\n        ));\n        fileList.Sort((a, b) =&gt;\n        {\n            if (a.IsDirectory != b.IsDirectory)\n                return a.IsDirectory ? -1 : 1;\n            return string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);\n        });\n\n        \/\/ 2. \u751f\u6210\u9762\u5305\u5c51\u5bfc\u822a\uff08\u652f\u6301\u591a\u5c42\u76ee\u5f55\u8df3\u8f6c\uff09\n        var breadcrumbBuilder = new StringBuilder();\n        string currentPath = \"\/\";\n        breadcrumbBuilder.Append($\"&lt;a href=\\\"{HtmlEncode(currentPath)}\\\"&gt;\/&lt;\/a&gt;\");\n        var requestPath = context.Request.Path.Value;\n        if (!string.IsNullOrEmpty(requestPath))\n        {\n            var pathSegments = requestPath.Split('\/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);\n            foreach (var segment in pathSegments)\n            {\n                currentPath = Path.Combine(currentPath, segment + \"\/\").Replace(Path.DirectorySeparatorChar, '\/');\n                breadcrumbBuilder.Append($\"&lt;a href=\\\"{HtmlEncode(currentPath)}\\\"&gt;{HtmlEncode(segment)}\/&lt;\/a&gt;\");\n            }\n        }\n\n        \/\/ 3. \u6784\u5efa HTML \u9875\u9762\uff08\u542b\u6837\u5f0f + \u6392\u5e8f\u811a\u672c\uff09\n        var htmlBuilder = new StringBuilder(4096);\n        htmlBuilder.AppendLine(\"&lt;!DOCTYPE html&gt;\");\n        htmlBuilder.AppendLine(\"&lt;html lang=\\\"zh-CN\\\"&gt;\");\n        htmlBuilder.AppendLine(\"&lt;head&gt;\");\n        htmlBuilder.AppendLine($\"&lt;title&gt;{_pageTitle} - {HtmlEncode(requestPath ?? \"\/\")}&lt;\/title&gt;\");\n        \/\/ \u6837\u5f0f\uff1a\u4fdd\u6301\u7b80\u6d01\uff0c\u9002\u914d\u4e0d\u540c\u8bbe\u5907\n        htmlBuilder.AppendLine(@\"&lt;style&gt;\n            body { font-family: \"\"Segoe UI\"\", \"\"Microsoft YaHei\"\", sans-serif; font-size: 14px; max-width: 1200px; margin: 0 auto; padding: 20px; }\n            header h1 { font-size: 24px; font-weight: 400; margin: 0 0 20px 0; color: #333; }\n            #index { width: 100%; border-collapse: separate; border-spacing: 0; border: 1px solid #eee; }\n            #index th { background: #f8f9fa; padding: 10px; text-align: center; cursor: pointer; user-select: none; position: relative; border-bottom: 2px solid #ddd; }\n            #index td { padding: 8px 10px; border-bottom: 1px solid #eee; }\n            #index th:hover { background: #f1f3f5; }\n            #index td.length, td.modified { text-align: right; }\n            a { color: #127aac; text-decoration: none; }\n            a:hover { color: #13709e; text-decoration: underline; }\n            .sort-arrow { position: absolute; right: 8px; font-size: 0.8em; color: #999; }\n            .dir-name { font-weight: 500; }\n        &lt;\/style&gt;\");\n        htmlBuilder.AppendLine(\"&lt;\/head&gt;\");\n        htmlBuilder.AppendLine(\"&lt;body&gt;\");\n        htmlBuilder.AppendLine($\"&lt;header&gt;&lt;h1&gt;{_pageTitle}\uff1a{breadcrumbBuilder}&lt;\/h1&gt;&lt;\/header&gt;\");\n        htmlBuilder.AppendLine(\"&lt;table id=\\\"index\\\"&gt;\");\n        htmlBuilder.AppendLine(\"&lt;thead&gt;&lt;tr&gt;\");\n        htmlBuilder.AppendLine(\"&lt;th data-col=\\\"name\\\"&gt;\u540d\u79f0 &lt;span class=\\\"sort-arrow\\\"&gt;\u2191&lt;\/span&gt;&lt;\/th&gt;\");\n        htmlBuilder.AppendLine(\"&lt;th data-col=\\\"size\\\"&gt;\u5927\u5c0f &lt;span class=\\\"sort-arrow\\\"&gt;&lt;\/span&gt;&lt;\/th&gt;\");\n        htmlBuilder.AppendLine(\"&lt;th data-col=\\\"modified\\\"&gt;\u6700\u540e\u4fee\u6539\u65f6\u95f4 &lt;span class=\\\"sort-arrow\\\"&gt;&lt;\/span&gt;&lt;\/th&gt;\");\n        htmlBuilder.AppendLine(\"&lt;\/tr&gt;&lt;\/thead&gt;&lt;tbody&gt;\");\n\n        \/\/ 4. \u751f\u6210\u6587\u4ef6\/\u76ee\u5f55\u5217\u8868\u884c\n        if (fileList.Count == 0)\n        {\n            htmlBuilder.AppendLine(\"&lt;tr&gt;&lt;td colspan=\\\"3\\\" style=\\\"text-align:center; padding:20px; color:#666;\\\"&gt;\u76ee\u5f55\u65e0\u53ef\u7528\u6587\u4ef6&lt;\/td&gt;&lt;\/tr&gt;\");\n        }\n        else\n        {\n            foreach (var file in fileList)\n            {\n                var fileName = file.IsDirectory ? $\"{file.Name}\/\" : file.Name;\n                var fileClass = file.IsDirectory ? \"dir-name\" : \"\";\n                var fileSize = file.IsDirectory ? \"-\" : FormatFileSizeWithComma(file.Length);\n                var fileModified = file.LastModified.ToUniversalTime().ToString(CustomDateTimeFormat, CultureInfo.InvariantCulture);\n                var encodedFileName = Uri.EscapeDataString(file.Name);\n                var fileUrl = $\"{context.Request.Path}\/{encodedFileName}\".Replace(\"\/\/\", \"\/\");\n\n                htmlBuilder.AppendLine(\"&lt;tr&gt;\");\n                htmlBuilder.AppendLine($\"&lt;td class=\\\"name {fileClass}\\\"&gt;&lt;a href=\\\"{HtmlEncode(fileUrl)}\\\"&gt;{HtmlEncode(fileName)}&lt;\/a&gt;&lt;\/td&gt;\");\n                htmlBuilder.AppendLine($\"&lt;td class=\\\"length\\\"&gt;{HtmlEncode(fileSize)}&lt;\/td&gt;\");\n                htmlBuilder.AppendLine($\"&lt;td class=\\\"modified\\\"&gt;{HtmlEncode(fileModified)}&lt;\/td&gt;\");\n                htmlBuilder.AppendLine(\"&lt;\/tr&gt;\");\n            }\n        }\n\n        htmlBuilder.AppendLine(\"&lt;\/tbody&gt;&lt;\/table&gt;\");\n        \/\/ 5. \u6392\u5e8f\u6838\u5fc3\u811a\u672c\uff08\u70b9\u51fb\u8868\u5934\u5207\u6362\u6392\u5e8f\uff09\n        htmlBuilder.AppendLine(@\"&lt;script&gt;\n            document.addEventListener('DOMContentLoaded', function() {\n                const table = document.getElementById('index');\n                if (!table) return;\n                const headers = table.querySelectorAll('thead th&#91;data-col]');\n                const tbody = table.querySelector('tbody');\n                let currentSort = { col: 'name', order: 'asc' };\n\n                \/\/ \u8868\u5934\u70b9\u51fb\u4e8b\u4ef6\n                headers.forEach(th =&gt; {\n                    th.addEventListener('click', function() {\n                        const newCol = this.dataset.col;\n                        currentSort.order = (currentSort.col === newCol) ? (currentSort.order === 'asc' ? 'desc' : 'asc') : 'asc';\n                        currentSort.col = newCol;\n                        updateSortArrows();\n                        sortTable();\n                    });\n                });\n\n                \/\/ \u66f4\u65b0\u6392\u5e8f\u7bad\u5934\n                function updateSortArrows() {\n                    headers.forEach(th =&gt; {\n                        const arrow = th.querySelector('.sort-arrow');\n                        arrow.textContent = (th.dataset.col === currentSort.col) ? (currentSort.order === 'asc' ? '\u2191' : '\u2193') : '';\n                    });\n                }\n\n                \/\/ \u6392\u5e8f\u903b\u8f91\n                function sortTable() {\n                    const rows = Array.from(tbody.querySelectorAll('tr'));\n                    if (!rows.length) return;\n\n                    rows.sort((a, b) =&gt; {\n                        const cellA = a.querySelector(`td.${getCellClass(currentSort.col)}`).textContent.trim();\n                        const cellB = b.querySelector(`td.${getCellClass(currentSort.col)}`).textContent.trim();\n\n                        switch (currentSort.col) {\n                            case 'name':\n                                const isDirA = cellA.endsWith('\/');\n                                const isDirB = cellB.endsWith('\/');\n                                if (isDirA !== isDirB) return isDirA ? -1 : 1;\n                                return currentSort.order === 'asc' ? cellA.localeCompare(cellB, 'zh-CN') : cellB.localeCompare(cellA, 'zh-CN');\n                            case 'size':\n                                if (cellA === '-') return -1;\n                                if (cellB === '-') return 1;\n                                const sizeA = BigInt(cellA.replace(\/,\/g, ''));\n                                const sizeB = BigInt(cellB.replace(\/,\/g, ''));\n                                return currentSort.order === 'asc' ? (sizeA &lt; sizeB ? -1 : 1) : (sizeA &gt; sizeB ? -1 : 1);\n                            case 'modified':\n                                const dateA = new Date(cellA.replace(' +00:00', 'Z')).getTime();\n                                const dateB = new Date(cellB.replace(' +00:00', 'Z')).getTime();\n                                return currentSort.order === 'asc' ? (dateA - dateB) : (dateB - dateA);\n                            default: return 0;\n                        }\n                    });\n\n                    tbody.innerHTML = '';\n                    rows.forEach(row =&gt; tbody.appendChild(row));\n                }\n\n                function getCellClass(colName) {\n                    return colName === 'name' ? 'name' : colName === 'size' ? 'length' : 'modified';\n                }\n\n                updateSortArrows();\n            });\n        &lt;\/script&gt;\");\n        htmlBuilder.AppendLine(\"&lt;\/body&gt;&lt;\/html&gt;\");\n\n        \/\/ \u8f93\u51fa\u54cd\u5e94\n        context.Response.ContentType = \"text\/html; charset=utf-8\";\n        await context.Response.WriteAsync(htmlBuilder.ToString(), Encoding.UTF8);\n    }\n\n    \/\/ \u8f85\u52a9\u65b9\u6cd5\uff1a\u6587\u4ef6\u5927\u5c0f\u683c\u5f0f\u5316\uff08\u5e26\u5343\u5206\u4f4d\uff09\n    private static string FormatFileSizeWithComma(long bytes)\n    {\n        return bytes.ToString(\"N0\", CultureInfo.InvariantCulture);\n    }\n\n    \/\/ \u8f85\u52a9\u65b9\u6cd5\uff1aHTML \u7f16\u7801\uff08\u9632 XSS\uff09\n    private static string HtmlEncode(string value)\n    {\n        if (string.IsNullOrEmpty(value)) return string.Empty;\n        return value.Replace(\"&amp;\", \"&amp;amp;\")\n                    .Replace(\"&lt;\", \"&amp;lt;\")\n                    .Replace(\"&gt;\", \"&amp;gt;\")\n                    .Replace(\"\\\"\", \"&amp;quot;\")\n                    .Replace(\"'\", \"&amp;#39;\")\n                    .Replace(\"`\", \"&amp;#96;\");\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6838\u5fc3\u529f\u80fd\u8bf4\u660e<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 \u9759\u6001\u6587\u4ef6\u670d\u52a1\u914d\u7f6e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PhysicalFileProvider(exposeDir)<\/code>\uff1a\u6307\u5b9a\u8981\u66b4\u9732\u7684\u7269\u7406\u76ee\u5f55\uff08\u5982 <code>GeoIP<\/code> \u6587\u4ef6\u5939\uff0c\u4f4d\u4e8e\u7a0b\u5e8f\u8fd0\u884c\u76ee\u5f55\u4e0b\uff09\uff1b<\/li>\n\n\n\n<li><code>RequestPath = \"\/geoip\"<\/code>\uff1a\u5ba2\u6237\u7aef\u901a\u8fc7 <code>http:\/\/localhost:5000\/geoip\/\u6587\u4ef6\u540d<\/code> \u8bbf\u95ee\u6587\u4ef6\uff1b<\/li>\n\n\n\n<li><code>OnPrepareResponse<\/code>\uff1a\u53ef\u9009\u914d\u7f6e\uff0c\u6dfb\u52a0\u7f13\u5b58\u63a7\u5236\u5934\uff0c\u907f\u514d\u6d4f\u89c8\u5668\u7f13\u5b58\u9759\u6001\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 \u76ee\u5f55\u6d4f\u89c8\u529f\u80fd<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>UseDirectoryBrowser<\/code>\uff1a\u542f\u7528\u76ee\u5f55\u6d4f\u89c8\uff08\u9ed8\u8ba4\u7981\u7528\uff09\uff0c\u9700\u4e0e\u9759\u6001\u6587\u4ef6\u670d\u52a1\u7684 <code>FileProvider<\/code> \u548c <code>RequestPath<\/code> \u4fdd\u6301\u4e00\u81f4\uff1b<\/li>\n\n\n\n<li><code>SortableDirectoryFormatter<\/code>\uff1a\u81ea\u5b9a\u4e49\u76ee\u5f55\u5217\u8868\u683c\u5f0f\u5316\u5668\uff0c\u66ff\u4ee3\u9ed8\u8ba4\u7684\u7b80\u964b\u5217\u8868\uff0c\u652f\u6301\u6392\u5e8f\u548c\u6837\u5f0f\u4f18\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.3 \u6392\u5e8f\u529f\u80fd\u5b9e\u73b0<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u524d\u7aef\uff1a\u901a\u8fc7 JavaScript \u76d1\u542c\u8868\u5934\u70b9\u51fb\u4e8b\u4ef6\uff0c\u5207\u6362\u6392\u5e8f\u5b57\u6bb5\uff08\u540d\u79f0\/\u5927\u5c0f\/\u4fee\u6539\u65f6\u95f4\uff09\u548c\u6392\u5e8f\u65b9\u5411\uff08\u5347\u5e8f\/\u964d\u5e8f\uff09\uff1b<\/li>\n\n\n\n<li>\u6392\u5e8f\u903b\u8f91\uff1a\n<ul class=\"wp-block-list\">\n<li>\u540d\u79f0\u6392\u5e8f\uff1a\u76ee\u5f55\u4f18\u5148\uff0c\u6587\u4ef6\u5728\u540e\uff0c\u6309\u5b57\u6bcd\/\u4e2d\u6587\u62fc\u97f3\u6392\u5e8f\uff1b<\/li>\n\n\n\n<li>\u5927\u5c0f\u6392\u5e8f\uff1a\u76ee\u5f55\u663e\u793a <code>-<\/code>\uff0c\u6587\u4ef6\u6309\u6570\u503c\u5927\u5c0f\u6392\u5e8f\uff08\u53bb\u9664\u5343\u5206\u4f4d\u9017\u53f7\uff09\uff1b<\/li>\n\n\n\n<li>\u65f6\u95f4\u6392\u5e8f\uff1a\u5c06 UTC \u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u6392\u5e8f\uff0c\u517c\u5bb9 <code>2025-10-18 08:16:06 +00:00<\/code> \u683c\u5f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5173\u952e\u4f18\u5316\u70b9\uff08\u517c\u5bb9\u751f\u4ea7\u73af\u5883\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u5168\u6027\u4f18\u5316<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>XSS \u9632\u62a4\uff1a\u901a\u8fc7 <code>HtmlEncode<\/code> \u65b9\u6cd5\u5bf9\u6587\u4ef6\u540d\u3001\u8def\u5f84\u7b49\u7528\u6237\u53ef\u63a7\u5185\u5bb9\u8fdb\u884c\u7f16\u7801\uff0c\u907f\u514d\u811a\u672c\u6ce8\u5165\uff1b<\/li>\n\n\n\n<li>\u9690\u85cf\u6587\u4ef6\u8fc7\u6ee4\uff1a\u9ed8\u8ba4\u8fc7\u6ee4\u4ee5 <code>.<\/code> \u5f00\u5934\u7684\u6587\u4ef6\uff08\u5982 <code>.gitignore<\/code>\u3001<code>.env<\/code>\uff09\uff0c\u9632\u6b62\u654f\u611f\u6587\u4ef6\u6cc4\u9732\uff1b<\/li>\n\n\n\n<li>\u53ef\u9009\uff1a\u6dfb\u52a0\u8eab\u4efd\u8ba4\u8bc1\uff08\u5982 API Key\u3001JWT\uff09\uff0c\u9650\u5236\u76ee\u5f55\u6d4f\u89c8\u6743\u9650\uff08\u4f8b\uff1a<code>RequireAuthorization()<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u517c\u5bb9\u6027\u4f18\u5316<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8de8\u5e73\u53f0\u652f\u6301\uff1a\u4f7f\u7528 <code>Path.Combine<\/code> \u548c\u8def\u5f84\u5206\u9694\u7b26\u66ff\u6362\uff0c\u517c\u5bb9 Windows\/Linux\/macOS\uff1b<\/li>\n\n\n\n<li>AOT \u7f16\u8bd1\u517c\u5bb9\uff1a\u907f\u514d\u53cd\u5c04\u548c\u52a8\u6001\u4ee3\u7801\uff0c\u652f\u6301 .NET 8+ AOT \u7f16\u8bd1\uff08\u53ef\u76f4\u63a5\u53d1\u5e03\u4e3a\u539f\u751f\u53ef\u6267\u884c\u6587\u4ef6\uff09\uff1b<\/li>\n\n\n\n<li>\u7a7a\u503c\u5b89\u5168\uff1a\u5904\u7406 <code>contents<\/code> \u4e3a <code>null<\/code>\u3001\u6587\u4ef6\u540d\u4e3a\u7a7a\u7b49\u5f02\u5e38\u573a\u666f\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f53\u9a8c\u4f18\u5316<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9762\u5305\u5c51\u5bfc\u822a\uff1a\u652f\u6301\u591a\u5c42\u76ee\u5f55\u8df3\u8f6c\uff08\u5982 <code>\/geoip\/GeoLite2\/<\/code>\uff09\uff0c\u65b9\u4fbf\u7528\u6237\u56de\u6eaf\uff1b<\/li>\n\n\n\n<li>\u65f6\u95f4\u683c\u5f0f\u6807\u51c6\u5316\uff1a\u7edf\u4e00\u4f7f\u7528 <code>yyyy-MM-dd HH:mm:ss +00:00<\/code> \u683c\u5f0f\uff0c\u907f\u514d\u65f6\u533a\u6df7\u6dc6\uff1b<\/li>\n\n\n\n<li>\u54cd\u5e94\u5934\u4f18\u5316\uff1a\u6307\u5b9a <code>charset=utf-8<\/code>\uff0c\u786e\u4fdd\u4e2d\u6587\u6587\u4ef6\u540d\u6b63\u5e38\u663e\u793a\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6d4b\u8bd5\u6548\u679c<\/h2>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u8fd0\u884c\u9879\u76ee\uff1a<code>dotnet run<\/code>\uff1b<\/li>\n\n\n\n<li>\u5728\u7a0b\u5e8f\u8fd0\u884c\u76ee\u5f55\u4e0b\u521b\u5efa <code>GeoIP<\/code> \u6587\u4ef6\u5939\uff0c\u653e\u5165\u6d4b\u8bd5\u6587\u4ef6\/\u5b50\u76ee\u5f55\uff1b<\/li>\n\n\n\n<li>\u8bbf\u95ee <code>http:\/\/localhost:5000\/geoip<\/code>\uff0c\u5373\u53ef\u770b\u5230\u76ee\u5f55\u5217\u8868\uff1a\n<ol class=\"wp-block-list\">\n<li>\u9ed8\u8ba4\u6309\u540d\u79f0\u5347\u5e8f\u6392\u5217\uff0c\u76ee\u5f55\u5728\u524d\uff0c\u6587\u4ef6\u5728\u540e\uff1b<\/li>\n\n\n\n<li>\u70b9\u51fb\u8868\u5934\u300c\u540d\u79f0\u300d\u300c\u5927\u5c0f\u300d\u300c\u6700\u540e\u4fee\u6539\u65f6\u95f4\u300d\u53ef\u5207\u6362\u6392\u5e8f\u65b9\u5f0f\uff1b<\/li>\n\n\n\n<li>\u70b9\u51fb\u6587\u4ef6\u540d\/\u76ee\u5f55\u540d\u53ef\u76f4\u63a5\u8bbf\u95ee\uff08\u76ee\u5f55\u4f1a\u8fdb\u5165\u4e0b\u4e00\u7ea7\u5217\u8868\uff09\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u6269\u5c55\u573a\u666f<\/h2>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u9759\u6001\u8d44\u6e90\u6258\u7ba1\uff1a\u7528\u4e8e\u6258\u7ba1 API \u6587\u6863\uff08\u5982 Swagger\u3001Redoc\uff09\u3001\u524d\u7aef\u9759\u6001\u6587\u4ef6\uff08Vue\/React \u6784\u5efa\u4ea7\u7269\uff09\uff1b<\/li>\n\n\n\n<li>\u5185\u90e8\u6587\u4ef6\u5171\u4eab\uff1a\u56e2\u961f\u5185\u90e8\u4e34\u65f6\u5171\u4eab\u6587\u4ef6\uff08\u7ed3\u5408\u8eab\u4efd\u8ba4\u8bc1\uff0c\u907f\u514d\u516c\u5f00\u8bbf\u95ee\uff09\uff1b<\/li>\n\n\n\n<li>\u65e5\u5fd7\u6587\u4ef6\u67e5\u770b\uff1a\u66b4\u9732\u65e5\u5fd7\u76ee\u5f55\uff0c\u652f\u6301\u6309\u4fee\u6539\u65f6\u95f4\u6392\u5e8f\u67e5\u770b\u6700\u65b0\u65e5\u5fd7\u6587\u4ef6\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u603b\u7ed3<\/h2>\n\n\n\n<p>\u901a\u8fc7 .NET MiniAPI \u7684\u9759\u6001\u6587\u4ef6\u4e2d\u95f4\u4ef6 + \u81ea\u5b9a\u4e49\u76ee\u5f55\u683c\u5f0f\u5316\u5668\uff0c\u6211\u4eec\u4ec5\u9700\u5c11\u91cf\u4ee3\u7801\u5c31\u5b9e\u73b0\u4e86\u300c\u6307\u5b9a\u76ee\u5f55\u66b4\u9732 + \u53ef\u6392\u5e8f\u76ee\u5f55\u6d4f\u89c8\u300d\u529f\u80fd\u3002\u8be5\u65b9\u6848\u7b80\u6d01\u9ad8\u6548\uff0c\u517c\u5bb9\u751f\u4ea7\u73af\u5883\uff0c\u9002\u7528\u4e8e\u8f7b\u91cf\u7ea7\u6587\u4ef6\u670d\u52a1\u573a\u666f\u3002\u5982\u9700\u8fdb\u4e00\u6b65\u589e\u5f3a\uff0c\u53ef\u6269\u5c55\u6743\u9650\u63a7\u5236\u3001\u6587\u4ef6\u4e0a\u4f20\u3001\u4e0b\u8f7d\u9650\u901f\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\"><strong>\u4e0b\u8f7d<\/strong><\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Demo<\/mark> : <a href=\"\/d\/MiniApiStaticFileDemo.zip\">MiniApiStaticFileDemo.zip<\/a><\/p>\n\n\n\n<p>\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\u6216\u4f18\u5316\u5efa\u8bae\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u4ea4\u6d41\uff5e<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u53d1\u8f7b\u91cf\u7ea7 API \u65f6\uff0c.NET MiniAPI \u51ed\u501f\u5176\u7b80\u6d01\u9ad8\u6548\u7684\u7279\u6027\u6210\u4e3a\u9996\u9009\u3002\u4f46\u5b9e\u9645\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u5e38\u9700\u8981\u66b4\u9732 &hellip; <a href=\"https:\/\/bohu.net\/blog\/10723\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5728 .NET MiniAPI (Kestrel) \u4e2d\u5b9e\u73b0\u9759\u6001\u6587\u4ef6\u670d\u52a1 + \u53ef\u6392\u5e8f\u76ee\u5f55\u6d4f\u89c8\u529f\u80fd\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":10724,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,4,34,11,693,26],"tags":[50,98,849,850,851,852,853,854,855],"class_list":{"0":"post-10723","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-net","8":"category-c-net","9":"category-html","10":"category-web","12":"category-26","13":"tag-net","14":"tag-c","15":"tag-formatter","16":"tag-kestrel","17":"tag-miniapi","18":"tag-usedirectorybrowser","19":"tag-usestaticfiles","20":"tag-854","21":"tag-855"},"_links":{"self":[{"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/posts\/10723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/comments?post=10723"}],"version-history":[{"count":1,"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/posts\/10723\/revisions"}],"predecessor-version":[{"id":10726,"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/posts\/10723\/revisions\/10726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/media\/10724"}],"wp:attachment":[{"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/media?parent=10723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/categories?post=10723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bohu.net\/blog\/wp-json\/wp\/v2\/tags?post=10723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}