HttpContext.RewritePath 方法

定義

將資源的要求重新導向至與要求的 URL 所指不同的路徑。 RewritePath 是在無 Cookie 工作階段狀態中用來刪除 URL 的工作階段識別碼。

多載

RewritePath(String, String, String, Boolean)

使用指定的虛擬路徑、路徑資訊、查詢字串資訊和布林值重寫 URL,這個布林值會指定用戶端檔案路徑是否已設為重寫路徑。

RewritePath(String, String, String)

使用指定的路徑、路徑資訊和查詢字串資訊重寫 URL。

RewritePath(String, Boolean)

使用指定的路徑和布林值重寫 URL,這個布林值會指定是否已修改伺服器資源的虛擬路徑。

RewritePath(String)

使用指定的路徑重寫 URL。

RewritePath(String, String, String, Boolean)

使用指定的虛擬路徑、路徑資訊、查詢字串資訊和布林值重寫 URL,這個布林值會指定用戶端檔案路徑是否已設為重寫路徑。

public:
 void RewritePath(System::String ^ filePath, System::String ^ pathInfo, System::String ^ queryString, bool setClientFilePath);
public void RewritePath (string filePath, string pathInfo, string queryString, bool setClientFilePath);
member this.RewritePath : string * string * string * bool -> unit
Public Sub RewritePath (filePath As String, pathInfo As String, queryString As String, setClientFilePath As Boolean)

參數

filePath
String

對要求提供服務之資源的虛擬路徑。

pathInfo
String

用於 URL 重新導向的其他路徑資訊。 如需詳細資訊,請參閱PathInfo

queryString
String

用於 URL 重新導向的要求查詢字串。

setClientFilePath
Boolean

true 表示要將用戶端資源所使用的檔案路徑設為 filePath 參數的值;否則為 false

例外狀況

path 參數不在目前應用程式的根目錄中。

filePath 參數不在目前應用程式的根目錄中。

範例

如需程式碼範例,請參閱 RewritePath(String) 方法多載。

備註

參數 filePath 不包含 參數的內容 pathInfo 。 針對 URL http://www.microsoft.com/virdir/page.html/tail ,參數 filePathhttp://www.microsoft.com/virdir/page.html ,而 pathInfo 參數為 tail。

若要確保不會修改用來建構資源路徑的虛擬路徑,請將 setClientFilePath 參數設定為 false 。 您可能想要設定 setClientFilePathfalse 的常見案例是當您需要重寫 URL,而您使用主題並將 URL 重新導向至位於與要求資源不同的資料夾中的資源。

當您想要重新建構 Web 應用程式中的頁面時,URL 重寫很有用,而且您想要確保已將舊 URL 加入書簽的人員仍然可以在移動頁面之後使用它們。 URL 重寫可讓您以透明方式將要求轉送至新的頁面位置。

如果您想要讓網站使用更方便使用的 URL,並且已針對搜尋引擎進行優化,則更健全的替代方法是使用 ASP.NET 路由。 如需詳細資訊,請參閱ASP.NET 路由

另請參閱

適用於

RewritePath(String, String, String)

使用指定的路徑、路徑資訊和查詢字串資訊重寫 URL。

public:
 void RewritePath(System::String ^ filePath, System::String ^ pathInfo, System::String ^ queryString);
public void RewritePath (string filePath, string pathInfo, string queryString);
member this.RewritePath : string * string * string -> unit
Public Sub RewritePath (filePath As String, pathInfo As String, queryString As String)

參數

filePath
String

內部重寫路徑。

pathInfo
String

資源的額外路徑資訊。 如需詳細資訊,請參閱PathInfo

queryString
String

要求查詢字串。

例外狀況

path 參數不在目前應用程式的根目錄中。

filePath 參數不在目前應用程式的根目錄中。

範例

如需程式碼範例,包括這個方法多載的範例,請參閱 RewritePath(String) 方法多載。

備註

方法 RewritePath 會將資源的要求重新導向至另一個資源,而不需變更 URL。

參數 filePath 不包含 pathInfo 參數內容。 針對 URL http://www.microsoft.com/virdir/page.html/tail ,參數 filePathhttp://www.microsoft.com/virdir/page.html ,而 pathInfo 參數為 tail。

當您想要重新建構 Web 應用程式中的頁面時,URL 重寫很有用,而且您想要確保已將舊 URL 加入書簽的人員仍然可以在移動頁面之後使用它們。 URL 重寫可讓您以透明方式將要求轉送至新的頁面位置。

如果您想要讓網站使用更方便使用的 URL,並且已針對搜尋引擎進行優化,則更健全的替代方法是使用 ASP.NET 路由。 如需詳細資訊,請參閱ASP.NET 路由

另請參閱

適用於

RewritePath(String, Boolean)

使用指定的路徑和布林值重寫 URL,這個布林值會指定是否已修改伺服器資源的虛擬路徑。

public:
 void RewritePath(System::String ^ path, bool rebaseClientPath);
public void RewritePath (string path, bool rebaseClientPath);
member this.RewritePath : string * bool -> unit
Public Sub RewritePath (path As String, rebaseClientPath As Boolean)

參數

path
String

內部重寫路徑。

rebaseClientPath
Boolean

true 表示要重設虛擬路徑;false 表示要保持虛擬路徑不變。

例外狀況

path 參數為 null

path 參數不在目前應用程式的根目錄中。

範例

如需程式碼範例,請參閱 RewritePath(String) 方法多載。

備註

方法 HttpContext.RewritePath(String, Boolean) 會呼叫 HttpContext.RewritePath(String) 方法, rebaseClientPath 並將 參數設定為 true 。 若要確保不會修改用來建構資源路徑的虛擬路徑,請將 rebaseClientPath 參數設定為 false 。 您可能想要設定 rebaseClientPathfalse 的常見案例是當您需要重寫 URL,而您使用主題並將 URL 重新導向至位於與要求資源不同的資料夾中的資源。

當您想要重新建構 Web 應用程式中的頁面時,URL 重寫很有用,而且您想要確保已將舊 URL 加入書簽的人員仍然可以在移動頁面之後使用它們。 URL 重寫可讓您以透明方式將要求轉送至新的頁面位置。

如果您想要讓網站使用更方便使用的 URL,並且已針對搜尋引擎進行優化,則更健全的替代方法是使用 ASP.NET 路由。 如需詳細資訊,請參閱ASP.NET 路由

另請參閱

適用於

RewritePath(String)

使用指定的路徑重寫 URL。

public:
 void RewritePath(System::String ^ path);
public void RewritePath (string path);
member this.RewritePath : string -> unit
Public Sub RewritePath (path As String)

參數

path
String

內部重寫路徑。

例外狀況

path 參數為 null

path 參數不在目前應用程式的根目錄中。

範例

下列範例示範如何使用 RewritePath 方法,讓網站回應未反映網站中檔案結構的 URL。 程式碼的第一個區塊是名為 RewritePath.aspx 的 ASP.NET 網頁。 它需要查詢字串。 如果網站的名稱是 WebSite1,URL http://localhost/WebSite1/RewritePath.aspx?page=1 就會在瀏覽器中顯示「第 1 頁」。 網頁後面的程式碼區塊是 Application_BeginRequest Global.asax 檔案中的事件處理常式。 此程式碼會攔截這類 http://localhost/WebSite1/page1 URL 的要求,並將其轉換為 RewritePath.aspx 在處理之前所需的表單。 因此,URL http://localhost/WebSite1/page1 會使用在瀏覽器中顯示 「Page 1」 的查詢字串參數叫用 RewritePath.aspx。 如果收到之類的 http://localhost/WebSite1/page1 URL,則會叫用 的多 RewritePath 載,讓您提供 屬性的值 PathInfo 以及查詢字串參數。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Page=" + Request.QueryString["page"] + " PathInfo=" + Request.PathInfo;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Label1.Text = "Page=" & Request.QueryString("page") & " PathInfo=" & Request.PathInfo
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>
void Application_BeginRequest(Object sender, EventArgs e)
{
    string originalPath = HttpContext.Current.Request.Path.ToLower();
    if (originalPath.Contains("/page1"))
    {
        Context.RewritePath(originalPath.Replace("/page1", "/RewritePath.aspx?page=page1"));
    }
    if (originalPath.Contains("/page2"))
    {
        Context.RewritePath(originalPath.Replace("/page2", "/RewritePath.aspx"), "pathinfo", "page=page2");
    }
}    
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim originalPath As String = HttpContext.Current.Request.Path.ToLower()
    If originalPath.Contains("/page1") Then
        Context.RewritePath(originalPath.Replace("/page1", "/RewritePath.aspx?page=page1"))
    End If
    If originalPath.Contains("/page2") Then
        Context.RewritePath(originalPath.Replace("/page2", "/RewritePath.aspx"), "pathinfo", "page=page2")
    End If
End Sub

備註

方法 RewritePath(String) 會將資源的要求重新導向至與所要求 URL 所指示的路徑不同的路徑。 如果您必須重設虛擬路徑,以便正確解析來自伺服器資源的用戶端要求,請使用這個方法的多載來接受 rebaseClientPath 參數,並將 參數設定為 false

當您想要重新建構 Web 應用程式中的頁面時,URL 重寫很有用,而且您想要確保已將舊 URL 加入書簽的人員仍然可以在移動頁面之後使用它們。 URL 重寫可讓您以透明方式將要求轉送至新的頁面位置。

如果您想要讓網站使用更方便使用的 URL,並且已針對搜尋引擎進行優化,則更健全的替代方法是使用 ASP.NET 路由。 如需詳細資訊,請參閱ASP.NET 路由

另請參閱

適用於