{"id":53185,"date":"2024-10-09T09:03:15","date_gmt":"2024-10-09T01:03:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/53185\/"},"modified":"2024-10-09T09:03:15","modified_gmt":"2024-10-09T01:03:15","slug":"asp-net-rewritepath-%e9%87%8d%e5%ae%9a%e5%90%91-http-%e9%a0%ad-content-location-%e6%9a%b4%e9%9c%b2%e7%9c%9f%e5%af%a6%e8%b7%af%e5%be%91%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%b3%95-2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/53185\/","title":{"rendered":"ASP.NET RewritePath \u91cd\u5b9a\u5411 HTTP \u982d Content-Location \u66b4\u9732\u771f\u5be6\u8def\u5f91\u89e3\u6c7a\u65b9\u6cd5"},"content":{"rendered":"<h1 id=\"asp-net-rewritepath-%e9%87%8d%e5%ae%9a%e5%90%91-http-%e9%a0%ad-content-location-%e6%9a%b4%e9%9c%b2%e7%9c%9f%e5%af%a6%e8%b7%af%e5%be%91%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%b3%95-sChIubWwxc\">ASP.NET RewritePath \u91cd\u5b9a\u5411 HTTP \u982d Content-Location \u66b4\u9732\u771f\u5be6\u8def\u5f91\u89e3\u6c7a\u65b9\u6cd5<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0cASP.NET \u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u6846\u67b6\uff0c\u7279\u5225\u662f\u5728\u69cb\u5efa\u52d5\u614b\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u65b9\u9762\u3002\u7576\u958b\u767c\u8005\u4f7f\u7528 ASP.NET \u9032\u884c URL \u91cd\u5beb\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u500b\u554f\u984c\uff1aHTTP \u982d\u4e2d\u7684 Content-Location \u53ef\u80fd\u6703\u66b4\u9732\u771f\u5be6\u7684\u8def\u5f91\u3002\u9019\u4e0d\u50c5\u5f71\u97ff\u4e86\u7db2\u7ad9\u7684\u5b89\u5168\u6027\uff0c\u9084\u53ef\u80fd\u5c0d SEO \u7522\u751f\u8ca0\u9762\u5f71\u97ff\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u9019\u4e00\u554f\u984c\u7684\u6839\u6e90\u53ca\u5176\u89e3\u6c7a\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"%e5%95%8f%e9%a1%8c%e7%9a%84%e6%a0%b9%e6%ba%90-sChIubWwxc\">\u554f\u984c\u7684\u6839\u6e90<\/h2>\n<p>\u5728 ASP.NET \u4e2d\uff0c\u4f7f\u7528 <code>RewritePath<\/code> \u65b9\u6cd5\u9032\u884c URL \u91cd\u5beb\u6642\uff0c\u7cfb\u7d71\u6703\u81ea\u52d5\u751f\u6210 HTTP \u982d\u90e8\u7684 Content-Location\u3002\u9019\u610f\u5473\u8457\u5373\u4f7f URL \u5df2\u7d93\u88ab\u91cd\u5beb\uff0cContent-Location \u4ecd\u7136\u6703\u6307\u5411\u539f\u59cb\u7684 URL\uff0c\u9019\u6a23\u5c31\u53ef\u80fd\u66b4\u9732\u51fa\u771f\u5be6\u7684\u8def\u5f91\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>Response.RewritePath(\"\/original\/path\");\n<\/code><\/pre>\n<p>\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0cHTTP \u982d\u90e8\u7684 Content-Location \u4ecd\u7136\u6703\u986f\u793a\u70ba <code>\/original\/path<\/code>\uff0c\u800c\u4e0d\u662f\u91cd\u5beb\u5f8c\u7684 URL\u3002\u9019\u6a23\u7684\u884c\u70ba\u53ef\u80fd\u6703\u5c0e\u81f4\u5b89\u5168\u6f0f\u6d1e\uff0c\u56e0\u70ba\u653b\u64ca\u8005\u53ef\u4ee5\u901a\u904e\u9019\u4e9b\u4fe1\u606f\u4f86\u63a8\u6e2c\u7db2\u7ad9\u7684\u7d50\u69cb\u3002<\/p>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-sChIubWwxc\">\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u70ba\u4e86\u89e3\u6c7a\u9019\u4e00\u554f\u984c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u5e7e\u7a2e\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e8%87%aa%e5%ae%9a%e7%be%a9-http-%e9%a0%ad%e9%83%a8-sChIubWwxc\">1. \u81ea\u5b9a\u7fa9 HTTP \u982d\u90e8<\/h3>\n<p>\u6700\u76f4\u63a5\u7684\u89e3\u6c7a\u65b9\u6848\u662f\u624b\u52d5\u8a2d\u7f6e HTTP \u982d\u90e8\uff0c\u8986\u84cb\u81ea\u52d5\u751f\u6210\u7684 Content-Location\u3002\u9019\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u4ee3\u78bc\u5be6\u73fe\uff1a<\/p>\n<pre><code>Response.RewritePath(\"\/new\/path\");\nResponse.Headers.Remove(\"Content-Location\");\nResponse.Headers.Add(\"Content-Location\", \"\/new\/path\");\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0cContent-Location \u5c07\u986f\u793a\u70ba\u91cd\u5beb\u5f8c\u7684 URL\uff0c\u800c\u4e0d\u662f\u539f\u59cb\u8def\u5f91\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8-url-%e9%87%8d%e5%af%ab%e6%a8%a1%e7%b5%84-sChIubWwxc\">2. \u4f7f\u7528 URL \u91cd\u5beb\u6a21\u7d44<\/h3>\n<p>\u5982\u679c\u4f7f\u7528\u7684\u662f IIS \u4f3a\u670d\u5668\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 URL \u91cd\u5beb\u6a21\u7d44\u3002\u9019\u500b\u6a21\u7d44\u63d0\u4f9b\u4e86\u66f4\u9748\u6d3b\u7684 URL \u91cd\u5beb\u529f\u80fd\uff0c\u4e26\u4e14\u53ef\u4ee5\u81ea\u52d5\u8655\u7406 HTTP \u982d\u90e8\u7684\u554f\u984c\u3002\u914d\u7f6e\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>&lt;rewrite&gt;\n    &lt;rules&gt;\n        &lt;rule name=\"Rewrite to new path\"&gt;\n            &lt;match url=\"^original\/path$\" \/&gt;\n            &lt;action type=\"Rewrite\" url=\"\/new\/path\" \/&gt;\n        &lt;\/rule&gt;\n    &lt;\/rules&gt;\n&lt;\/rewrite&gt;\n<\/code><\/pre>\n<p>\u9019\u6a23\u914d\u7f6e\u5f8c\uff0cIIS \u5c07\u81ea\u52d5\u8655\u7406 Content-Location \u982d\u90e8\uff0c\u907f\u514d\u66b4\u9732\u771f\u5be6\u8def\u5f91\u3002<\/p>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e4%b8%ad%e4%bb%8b%e8%bb%9f%e4%bb%b6-sChIubWwxc\">3. \u4f7f\u7528\u4e2d\u4ecb\u8edf\u4ef6<\/h3>\n<p>\u5728 ASP.NET Core \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e2d\u4ecb\u8edf\u4ef6\u4f86\u8655\u7406 HTTP \u8acb\u6c42\u548c\u97ff\u61c9\u3002\u9019\u6a23\u53ef\u4ee5\u5728\u8acb\u6c42\u9032\u5165\u63a7\u5236\u5668\u4e4b\u524d\u6216\u97ff\u61c9\u767c\u9001\u4e4b\u524d\u9032\u884c\u8655\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>public class CustomHeaderMiddleware\n{\n    private readonly RequestDelegate _next;\n\n    public CustomHeaderMiddleware(RequestDelegate next)\n    {\n        _next = next;\n    }\n\n    public async Task Invoke(HttpContext context)\n    {\n        await _next(context);\n        context.Response.Headers.Remove(\"Content-Location\");\n        context.Response.Headers.Add(\"Content-Location\", \"\/new\/path\");\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5728\u97ff\u61c9\u4e2d\u4e0d\u6703\u66b4\u9732\u771f\u5be6\u8def\u5f91\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-sChIubWwxc\">\u7e3d\u7d50<\/h2>\n<p>\u5728 ASP.NET \u4e2d\uff0c\u4f7f\u7528 <code>RewritePath<\/code> \u65b9\u6cd5\u9032\u884c URL \u91cd\u5beb\u6642\uff0cHTTP \u982d\u90e8\u7684 Content-Location \u53ef\u80fd\u6703\u66b4\u9732\u771f\u5be6\u8def\u5f91\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u4e00\u554f\u984c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9078\u64c7\u624b\u52d5\u8a2d\u7f6e HTTP \u982d\u90e8\u3001\u4f7f\u7528 URL \u91cd\u5beb\u6a21\u7d44\u6216\u4e2d\u4ecb\u8edf\u4ef6\u4f86\u8655\u7406\u3002\u9019\u4e9b\u65b9\u6cd5\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7db2\u7ad9\u7684\u5b89\u5168\u6027\uff0c\u9084\u80fd\u6539\u5584 SEO \u8868\u73fe\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u7db2\u7ad9\u6258\u7ba1\u9084\u662f\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6c7a ASP.NET \u4e2d RewritePath \u91cd\u5b9a\u5411\u6642 HTTP \u982d Content-Location \u66b4\u9732\u771f\u5be6\u8def\u5f91\u7684\u554f\u984c\uff0c\u63d0\u5347\u5b89\u5168\u6027\u8207\u96b1\u79c1\u4fdd\u8b77\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-53185","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/53185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=53185"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/53185\/revisions"}],"predecessor-version":[{"id":53186,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/53185\/revisions\/53186"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=53185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=53185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=53185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}