HttpContext.RewritePath 方法

定义

将对资源的请求重定向到另一路径,使其不同于所请求 URL 指示的路径。Redirects a request for a resource to a different path than the one that is indicated by the requested URL. RewritePath 用于无 Cookie 会话状态中,作用是从 URL 中剥离会话 ID。RewritePath is used in cookieless session state to strip session IDs from URLs.

重载

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)

使用给定的路径、路径信息和查询字符串信息重写 URL。Rewrites the URL by using the given path, path information, and query string information.

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)

使用给定路径重写 URL。Rewrites the URL using the given path.

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

服务请求的资源的虚拟路径。The virtual path to the resource that services the request.

pathInfo
String

用于 URL 重定向的附加路径信息。Additional path information to use for the URL redirect. 有关详细信息,请参阅 PathInfoFor more information, see PathInfo.

queryString
String

用于 URL 重定向的请求查询字符串。The request query string to use for the URL redirect.

setClientFilePath
Boolean

若要将用于客户端资源的文件路径设置为 filePath 参数的值,则为 true;否则为 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.

注解

filePath 参数不包括 pathInfo 参数的内容。The filePath parameter does not include the content of the pathInfo parameter. 对于 URL http://www.microsoft.com/virdir/page.html/tail,`filePath` 参数 http://www.microsoft.com/virdir/page.html,`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. 通常情况下,你可能想要将 setClientFilePath 设置为 false 是需要重写 URL,并使用主题并将 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 路由For more information, see ASP.NET Routing.

另请参阅

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

内部重写路径。The internal rewrite path.

pathInfo
String

资源的附加路径信息。Additional path information for a resource. 有关详细信息,请参阅 PathInfoFor more information, see PathInfo.

queryString
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,`filePath` 参数 http://www.microsoft.com/virdir/page.html,`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 路由For more information, see ASP.NET Routing.

另请参阅

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

内部重写路径。The internal rewrite path.

rebaseClientPath
Boolean

如果重置虚拟路径,则为 true;如果保持虚拟路径不变,则为 falsetrue to reset the virtual path; false to keep the virtual path unchanged.

异常

path 参数为 nullThe path parameter is null.

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.

注解

HttpContext.RewritePath(String, Boolean) 方法由 HttpContext.RewritePath(String) 方法调用,其 rebaseClientPath 参数设置为 trueThe 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. 通常情况下,你可能想要将 rebaseClientPath 设置为 false 是需要重写 URL,并使用主题并将 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 路由For more information, see ASP.NET Routing.

另请参阅

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

内部重写路径。The internal rewrite path.

异常

path 参数为 nullThe path parameter is null.

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. Web 页面后的代码块是 global.asax 文件中的 Application_BeginRequest 事件处理程序。The block of code that follows the Web page is the Application_BeginRequest event handler in the Global.asax file. 此代码会截获对 http://localhost/WebSite1/page1 的 Url 的请求,并将它们转换为 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 在浏览器中用显示 "第1页" 的查询字符串参数调用 RewritePath。Therefore, the URL http://localhost/WebSite1/page1 invokes RewritePath.aspx with the query-string parameter that displays "Page 1" in the browser. 如果收到 URL (如 http://localhost/WebSite1/page1),则将调用 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 参数并将参数设置为 falseIf 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 路由For more information, see ASP.NET Routing.

另请参阅

适用于