ASP.NET 2.0 引入Substitution 控件,解決「整頁快取」與「局部即時內容」的矛盾:在啟用頁面輸出快取的頁面中,指定特定區塊不快取、每次即時產生。同時新增URL 重寫/地址映射功能,支援友善網址與舊版相容。
典型場景:頁面底部版權資訊、導航選單可快取 30 秒;但股票走勢圖、即時新聞必須每次重新產生。
<%@ OutputCache Duration="30" VaryByParam="none" %>
<script runat="server" language="VB">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
CachedDate.Text = DateTime.Now.ToString()
End Sub
Shared Function GetCurrentDate(ByVal context As HttpContext) As String
Return DateTime.Now.ToString()
End Function
</script>
<html><body>
<form runat="server">
<p>下方時間 未使用快取,每次重新整理更新:</p>
<asp:Substitution ID="Substitution1" MethodName="GetCurrentDate" runat="server" />
<p>下方時間 使用快取,30 秒內不變:</p>
<asp:Label ID="CachedDateLabel" runat="server" />
<asp:Button ID="Refresh" Text="重新整理頁面" runat="server" />
</form>
</body></html>

圖示:Substitution 控件執行結果,上方時間即時更新,下方時間快取 30 秒
MethodName 指定 static/shared 方法,簽名為 String MethodName(HttpContext context)。ASP.NET 2.0 內建 HttpContext.RewritePath 與 web.config <urlMappings> 區段,支援:
/Products/Books/ASP.NET → /ProductList.aspx?Category=Books&Keyword=ASP.NET/OldPage.asp → /NewPage.aspx/About → /About.aspx<system.web>
<urlMappings enabled="true">
<add url="~/Products/Books/ASP.NET" mappedUrl="~/ProductList.aspx?Category=Books&Keyword=ASP.NET" />
<add url="~/OldPage.asp" mappedUrl="~/NewPage.aspx" />
</urlMappings>
</system.web>
Substitution 控件與 URL 映射,是 ASP.NET 2.0 在效能與架構靈活性上的兩大實用增強。