HttpContext.RewritePath HttpContext.RewritePath HttpContext.RewritePath HttpContext.RewritePath Method

定義

將資源的要求重新導向至與要求的 URL 所指不同的路徑。Redirects a request for a resource to a different path than the one that is indicated by the requested URL. RewritePath 是在無 Cookie 工作階段狀態中用來刪除 URL 的工作階段識別碼。RewritePath is used in cookieless session state to strip session IDs from URLs.

多載

RewritePath(String, String, String, Boolean) RewritePath(String, String, String, Boolean) RewritePath(String, String, String, Boolean) RewritePath(String, String, String, Boolean)

使用指定的虛擬路徑、路徑資訊、查詢字串資訊和布林值重寫 URL,這個布林值會指定用戶端檔案路徑是否已設定為重寫路徑。Rewrites the URL using the given virtual path, path information, query string information, and a Boolean value that specifies whether the client file path is set to the rewrite path.

RewritePath(String, String, String) RewritePath(String, String, String) RewritePath(String, String, String) RewritePath(String, String, String)

使用指定的路徑、路徑資訊和查詢字串資訊重新寫入 URL。Rewrites the URL by using the given path, path information, and query string information.

RewritePath(String, Boolean) RewritePath(String, Boolean) RewritePath(String, Boolean) RewritePath(String, Boolean)

使用指定的路徑和布林值重寫 URL,這個路徑和布林值會指定伺服器資源的虛擬路徑是否已修改。Rewrites the URL using the given path and a Boolean value that specifies whether the virtual path for server resources is modified.

RewritePath(String) RewritePath(String) RewritePath(String) RewritePath(String)

使用指定的路徑重寫 URL。Rewrites the URL using the given path.

RewritePath(String, String, String, Boolean) RewritePath(String, String, String, Boolean) RewritePath(String, String, String, Boolean) RewritePath(String, String, String, Boolean)

使用指定的虛擬路徑、路徑資訊、查詢字串資訊和布林值重寫 URL,這個布林值會指定用戶端檔案路徑是否已設定為重寫路徑。Rewrites the URL using the given virtual path, path information, query string information, and a Boolean value that specifies whether the client file path is set to the rewrite path.

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 String String String

為要求提供服務之資源的虛擬路徑。The virtual path to the resource that services the request.

pathInfo
String String String String

用於重新導向 URL 的額外路徑資訊。Additional path information to use for the URL redirect. 如需詳細資訊,請參閱PathInfoFor more information, see PathInfo.

queryString
String String String String

用於重新導向 URL 的要求查詢字串。The request query string to use for the URL redirect.

setClientFilePath
Boolean Boolean Boolean Boolean

true 表示將要用於用戶端資源的檔案路徑設為 filePath 參數的值,否則為 falsetrue to set the file path used for client resources to the value of the filePath parameter; otherwise false.

例外狀況

path 參數不在目前應用程式的根目錄中。The path parameter is not in the current application's root directory.

filePath 參數不在目前應用程式的根目錄中。The filePath parameter is not in the current application's root directory.

範例

如需程式碼範例, 請RewritePath(String)參閱方法多載。For a code example, see the RewritePath(String) method overload.

備註

參數不包含pathInfo參數的內容。 filePathThe filePath parameter does not include the content of the pathInfo parameter. 針對 URL http://www.microsoft.com/virdir/page.html/tail http://www.microsoft.com/virdir/page.html, filePath 參數為,而pathInfo參數為 tail。For the URL http://www.microsoft.com/virdir/page.html/tail, the filePath parameter is http://www.microsoft.com/virdir/page.html, and the pathInfo parameter is tail.

若要確保不會修改用來建立資源路徑的虛擬路徑, 請將setClientFilePath參數設定為。 falseTo ensure that the virtual path that is used to construct paths to resources is not modified, set the setClientFilePath parameter to false. 當您需要重新撰寫 URL, 而且您使用setClientFilePath主題false並將 url 重新導向至位於與所要求資源不同之資料夾中的資源時, 您可能會想要將設定為的常見案例。A common scenario in which you might want to set setClientFilePath to false is when you need to rewrite the URL, and you are using themes and redirecting the URL to a resource located in a different folder than the requested resource.

當您想要重新組織 Web 應用程式中的頁面, 而且想要確保已將舊 Url 加上書簽的人在移動頁面之後, 仍然可以使用 URL 重寫功能。URL rewriting is useful when you want to restructure the pages in your Web application, and you want to make sure that people who have bookmarked old URLs can still use them after you have moved pages. URL 重寫可讓您以透明方式將要求轉寄到新的頁面位置。URL rewriting enables you to transparently forward requests to the new page location.

如果您想要讓網站使用更方便使用的 Url, 並已針對搜尋引擎優化, 則更健全的替代方法是使用 ASP.NET 路由。If you want to enable a site to use URLs that are more user-friendly and are optimized for search engines, a more robust alternative is to use ASP.NET routing. 如需詳細資訊, 請參閱ASP.NET RoutingFor more information, see ASP.NET Routing.

另請參閱

RewritePath(String, String, String) RewritePath(String, String, String) RewritePath(String, String, String) RewritePath(String, String, String)

使用指定的路徑、路徑資訊和查詢字串資訊重新寫入 URL。Rewrites the URL by using the given path, path information, and query string information.

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 String String String

內部的重新寫入路徑。The internal rewrite path.

pathInfo
String String String String

資源的額外路徑資訊。Additional path information for a resource. 如需詳細資訊,請參閱PathInfoFor more information, see PathInfo.

queryString
String String String String

要求查詢字串。The request query string.

例外狀況

path 參數不在目前應用程式的根目錄中。The path parameter is not in the current application's root directory.

filePath 參數不在目前應用程式的根目錄中。The filePath parameter is not in the current application's root directory.

範例

如需程式碼範例 (包括此方法多載的範例), RewritePath(String)請參閱方法多載。For a code example, including an example of this method overload, see the RewritePath(String) method overload.

備註

RewritePath方法會將資源的要求重新導向至另一個資源, 而不會變更 URL。The RewritePath method redirects a request for a resource to another resource without changing the URL.

filePath參數不pathInfo包含參數內容。The filePath parameter does not include the pathInfo parameter content. 針對 URL http://www.microsoft.com/virdir/page.html/tail http://www.microsoft.com/virdir/page.html, filePath 參數為,而pathInfo參數為 tail。For the URL http://www.microsoft.com/virdir/page.html/tail, the filePath parameter is http://www.microsoft.com/virdir/page.html, and the pathInfo parameter is tail.

當您想要重新組織 Web 應用程式中的頁面, 而且想要確保已將舊 Url 加上書簽的人在移動頁面之後, 仍然可以使用 URL 重寫功能。URL rewriting is useful when you want to restructure the pages in your Web application, and you want to make sure that people who have bookmarked old URLs can still use them after you have moved pages. URL 重寫可讓您以透明方式將要求轉寄到新的頁面位置。URL rewriting enables you to transparently forward requests to the new page location.

如果您想要讓網站使用更方便使用的 Url, 並已針對搜尋引擎優化, 則更健全的替代方法是使用 ASP.NET 路由。If you want to enable a site to use URLs that are more user-friendly and are optimized for search engines, a more robust alternative is to use ASP.NET routing. 如需詳細資訊, 請參閱ASP.NET RoutingFor more information, see ASP.NET Routing.

另請參閱

RewritePath(String, Boolean) RewritePath(String, Boolean) RewritePath(String, Boolean) RewritePath(String, Boolean)

使用指定的路徑和布林值重寫 URL,這個路徑和布林值會指定伺服器資源的虛擬路徑是否已修改。Rewrites the URL using the given path and a Boolean value that specifies whether the virtual path for server resources is modified.

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 String String String

內部的重新寫入路徑。The internal rewrite path.

rebaseClientPath
Boolean Boolean Boolean Boolean

重設虛擬路徑為 true;虛擬路徑保持不變為 falsetrue to reset the virtual path; false to keep the virtual path unchanged.

例外狀況

path 參數不在目前應用程式的根目錄中。The path parameter is not in the current application's root directory.

範例

如需程式碼範例, 請RewritePath(String)參閱方法多載。For a code example, see the RewritePath(String) method overload.

備註

方法是rebaseClientPath由方法呼叫, 並將參數設定為trueHttpContext.RewritePath(String) HttpContext.RewritePath(String, Boolean)The HttpContext.RewritePath(String, Boolean) method is called by the HttpContext.RewritePath(String) method with the rebaseClientPath parameter set to true. 若要確保不會修改用來建立資源路徑的虛擬路徑, 請將rebaseClientPath參數設定為。 falseTo ensure that the virtual path that is used to construct paths to resources is not modified, set the rebaseClientPath parameter to false. 當您需要重新撰寫 URL, 而且您使用rebaseClientPath主題false並將 url 重新導向至位於與所要求資源不同之資料夾中的資源時, 您可能會想要將設定為的常見案例。A common scenario in which you might want to set rebaseClientPath to false is when you need to rewrite the URL, and you are using themes and redirecting the URL to a resource located in a different folder than the requested resource.

當您想要重新組織 Web 應用程式中的頁面, 而且想要確保已將舊 Url 加上書簽的人在移動頁面之後, 仍然可以使用 URL 重寫功能。URL rewriting is useful when you want to restructure the pages in your Web application, and you want to make sure that people who have bookmarked old URLs can still use them after you have moved pages. URL 重寫可讓您以透明方式將要求轉寄到新的頁面位置。URL rewriting enables you to transparently forward requests to the new page location.

如果您想要讓網站使用更方便使用的 Url, 並已針對搜尋引擎優化, 則更健全的替代方法是使用 ASP.NET 路由。If you want to enable a site to use URLs that are more user-friendly and are optimized for search engines, a more robust alternative is to use ASP.NET routing. 如需詳細資訊, 請參閱ASP.NET RoutingFor more information, see ASP.NET Routing.

另請參閱

RewritePath(String) RewritePath(String) RewritePath(String) RewritePath(String)

使用指定的路徑重寫 URL。Rewrites the URL using the given path.

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

參數

path
String String String String

內部的重新寫入路徑。The internal rewrite path.

例外狀況

path 參數不在目前應用程式的根目錄中。The path parameter is not in the current application's root directory.

範例

下列範例示範如何使用RewritePath方法, 讓網站回應不會反映網站中檔案結構的 url。The following example shows how to use the RewritePath method to enable a Web site to respond to URLs that do not reflect the file structure in the Web site. 程式碼的第一個區塊是名為 RewritePath 的 ASP.NET 網頁。The first block of code is an ASP.NET Web page that is named RewritePath.aspx. 它需要查詢字串。It requires a query string. 如果您的網站名稱是 WebSite1, 則 URL http://localhost/WebSite1/RewritePath.aspx?page=1會在瀏覽器中顯示「第1頁」。If the name of your site is WebSite1, the URL http://localhost/WebSite1/RewritePath.aspx?page=1 displays "Page 1" in the browser. 在網頁之後的程式碼區塊是 global.asax 檔案中Application_BeginRequest的事件處理常式。The block of code that follows the Web page is the Application_BeginRequest event handler in the Global.asax file. 此程式碼會攔截的 url http://localhost/WebSite1/page1要求, 並將它們轉換為 RewritePath 所需的格式, 然後才進行處理。This code intercepts requests for URLs such as http://localhost/WebSite1/page1 and converts them to the form that is required for RewritePath.aspx before they are processed. 因此, URL http://localhost/WebSite1/page1會叫用 RewritePath, 並在瀏覽器中顯示「第1頁」的查詢字串參數。Therefore, the URL http://localhost/WebSite1/page1 invokes RewritePath.aspx with the query-string parameter that displays "Page 1" in the browser. 如果收到如http://localhost/WebSite1/page1的 URL, 則會叫用的RewritePath多載, 以讓您PathInfo提供屬性的值以及查詢字串參數。If a URL such as http://localhost/WebSite1/page1 is received, an overload of RewritePath is invoked that enables you to provide a value for the PathInfo property as well as a query string parameter.

<%@ 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 所指示的路徑。The RewritePath(String) method redirects a request for a resource to a different path than the one that is indicated by the requested URL. 如果您必須重設虛擬路徑, 以便從伺服器資源的用戶端要求正確解析, 請使用此方法的多載, 它會rebaseClientPath接受參數並將參數設定false為。If you have to reset the virtual path so that requests from the client for server resources resolve correctly, use the overload of this method that takes the rebaseClientPath parameter and set the parameter to false.

當您想要重新組織 Web 應用程式中的頁面, 而且想要確保已將舊 Url 加上書簽的人在移動頁面之後, 仍然可以使用 URL 重寫功能。URL rewriting is useful when you want to restructure the pages in your Web application, and you want to make sure that people who have bookmarked old URLs can still use them after you have moved pages. URL 重寫可讓您以透明方式將要求轉寄到新的頁面位置。URL rewriting enables you to transparently forward requests to the new page location.

如果您想要讓網站使用更方便使用的 Url, 並已針對搜尋引擎優化, 則更健全的替代方法是使用 ASP.NET 路由。If you want to enable a site to use URLs that are more user-friendly and are optimized for search engines, a more robust alternative is to use ASP.NET routing. 如需詳細資訊, 請參閱ASP.NET RoutingFor more information, see ASP.NET Routing.

另請參閱

適用於