首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

IIS URL Rewriting 與 ASP.NET Routing 選型指南(下):協同應用實戰案例

     編輯:關於ASP.NET

選型原則

  • 非 ASP.NET 動態技術 → 必須用 IIS URL Rewrite Module。
  • ASP.NET MVC / Dynamic Data → 優先用 ASP.NET Routing (原生支援)。
  • 既有網站不想改動 → 用 URL Rewrite Module (不需修改程式碼)。

協同應用案例

強制規範 URL (Canonical URL)

將 https://mysite.com/Home/About 重導向至 https://www.mysite.com/home/about。Rewrite Module 處理重導向,Routing 處理 Controller 對應。

<rule name="Enforce canonical hostname" stopProcessing="true">
  <match url="(.*)" />
  <conditions><add input="{HTTP_HOST}" negate="true" pattern="^www\.mysite\.com$" /></conditions>
  <action type="Redirect" url="https://www.mysite.com/{R:1}" redirectType="Permanent" />
</rule>

靜態內容轉向獨立伺服器

圖片請求 ^.+\.(jpg|bmp|gif)$ 透過 Rewrite 轉至 https://static_file_server/{R:0},動態頁面由 Routing 處理。

<rule name="Forward to static file server">
  <match url="^.+\.(?:jpg|bmp|gif)$" />
  <action type="Rewrite" url="https://static_file_server/{R:0}" />
</rule>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved