共用方式為


HttpContext.RewritePath 方法

定義

將資源請求導向到與所請求 URL 所指示的不同路徑。 RewritePath 在無 cookie 會話狀態下,用來剝除 URL 中的會話 ID。

多載

名稱 Description
RewritePath(String, String, String, Boolean)

利用給定的虛擬路徑、路徑資訊、查詢字串資訊,以及一個布林值(指定用戶端檔案路徑是否設定為重寫路徑)來重寫 URL。

RewritePath(String, String, String)

透過使用給定的路徑、路徑資訊和查詢字串資訊來重寫 URL。

RewritePath(String, Boolean)

利用給定路徑及布林值重寫 URL,該值指定伺服器資源的虛擬路徑是否被修改。

RewritePath(String)

用給定路徑重寫網址。

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

提供用於網址重定向的額外路徑資訊。 如需詳細資訊,請參閱PathInfo

queryString
String

請求查詢字串用於 URL 重定向。

setClientFilePath
Boolean

true 將用於用戶端資源的檔案路徑設定為參數值 filePath ;否則 false

例外狀況

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

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

範例

關於程式碼範例,請參見 RewritePath(String) 方法過載。

備註

參數 filePath 不包含參數 pathInfo 的內容。 對於網址 http://www.microsoft.com/virdir/page.html/tail,參數 filePath 為 ,參數為 http://www.microsoft.com/virdir/page.htmlpathInfo 參數為尾端。

為確保用於構建資源路徑的虛擬路徑不會被修改,將參數設 setClientFilePathfalse。 一個常見setClientFilePathfalse的情況是,當你需要重寫 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 參數內容。 對於網址 http://www.microsoft.com/virdir/page.html/tail,參數 filePath 為 ,參數為 http://www.microsoft.com/virdir/page.htmlpathInfo 參數為尾端。

當你想在網頁應用程式中重組頁面時,網址重寫非常有用,且你要確保已收藏舊網址的人在你移動頁面後仍能使用它們。 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 保持虛擬路徑不變。

例外狀況

參數 pathnull

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

範例

關於程式碼範例,請參見 RewritePath(String) 方法過載。

備註

HttpContext.RewritePath(String, Boolean)該方法由參數rebaseClientPath設為 true的方法呼叫HttpContext.RewritePath(String)。 為確保用於構建資源路徑的虛擬路徑不會被修改,將參數設 rebaseClientPathfalse。 一個常見rebaseClientPathfalse的情況是,當你需要重寫 URL,且你使用主題並將 URL 導向到與請求資源不同資料夾的資源時。

當你想在網頁應用程式中重組頁面時,網址重寫非常有用,且你要確保已收藏舊網址的人在你移動頁面後仍能使用它們。 URL 重寫讓你能透明地將請求轉發到新的頁面位置。

如果你想讓網站使用更友善且對搜尋引擎優化的網址,更強大的替代方案是使用 ASP.NET 路由。 更多資訊請參見 ASP.NET 路由

另請參閱

適用於

RewritePath(String)

用給定路徑重寫網址。

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

參數

path
String

內部重寫路徑。

例外狀況

參數 pathnull

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

範例

以下範例說明如何使用此 RewritePath 方法,使網站能回應與網站檔案結構不符的網址。 第一個程式碼區塊是一個名為 RewritePath.aspx 的 ASP.NET 網頁。 它需要一個查詢字串。 如果您的網站名稱為 WebSite1,瀏覽器中的網址 http://localhost/WebSite1/RewritePath.aspx?page=1 會顯示「Page 1」。 網頁後面的程式碼區塊是 Application_BeginRequest Global.asax 檔案中的事件處理程式。 此程式碼攔截 URL http://localhost/WebSite1/page1 請求,並將其轉換為RewritePath.aspx所需的表單,然後再處理。 因此,該 URL http://localhost/WebSite1/page1 會以查詢字串參數呼叫 RewritePath.aspx,該參數在瀏覽器中顯示「第 1 頁」。 如果收到像 這樣的 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

當你想在網頁應用程式中重組頁面時,網址重寫非常有用,且你要確保已收藏舊網址的人在你移動頁面後仍能使用它們。 URL 重寫讓你能透明地將請求轉發到新的頁面位置。

如果你想讓網站使用更友善且對搜尋引擎優化的網址,更強大的替代方案是使用 ASP.NET 路由。 更多資訊請參見 ASP.NET 路由

另請參閱

適用於