通过


HttpContext.RewritePath 方法

定义

将资源请求重定向到与请求的 URL 指示的路径不同的路径。 RewritePath 用于无 Cookie 会话状态,以从 URL 中去除会话 ID。

重载

名称 说明
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

将用于客户端资源的文件路径设置为参数的值;否则为 < a0/>。

例外

参数 path 不在当前应用程序的根目录中。

参数 filePath 不在当前应用程序的根目录中。

示例

有关代码示例,请参阅 RewritePath(String) 方法重载。

注解

filePath 参数不包括参数的内容 pathInfo 。 对于 URL http://www.microsoft.com/virdir/page.html/tail,参数 filePathhttp://www.microsoft.com/virdir/page.html,参数 pathInfo 为结尾。

若要确保用于构造资源路径的虚拟路径未修改,请将参数设置为 < a0/>。 可能需要重新setClientFilePathfalse编写 URL 的常见方案是需要重写 URL,并且你正在使用主题并将 URL 重定向到位于请求的资源所在的资源。

如果要重构 Web 应用程序中的页面,并且希望确保已添加旧 URL 书签的人员在移动页面后仍可使用这些 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 为结尾。

如果要重构 Web 应用程序中的页面,并且希望确保已添加旧 URL 书签的人员在移动页面后仍可使用这些 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 若要使虚拟路径保持不变,则为 。

例外

参数 pathnull.

参数 path 不在当前应用程序的根目录中。

示例

有关代码示例,请参阅 RewritePath(String) 方法重载。

注解

该方法HttpContext.RewritePath(String, Boolean)HttpContext.RewritePath(String)参数设置为 true 的方法rebaseClientPath调用。 若要确保用于构造资源路径的虚拟路径未修改,请将参数设置为 < a0/>。 可能需要重新rebaseClientPathfalse编写 URL 的常见方案是需要重写 URL,并且你正在使用主题并将 URL 重定向到位于请求的资源所在的资源。

如果要重构 Web 应用程序中的页面,并且希望确保已添加旧 URL 书签的人员在移动页面后仍可使用这些 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

内部重写路径。

例外

参数 pathnull.

参数 path 不在当前应用程序的根目录中。

示例

以下示例演示如何使用 RewritePath 该方法使网站能够响应未反映网站中的文件结构的 URL。 第一个代码块是名为RewritePath.aspx的 ASP.NET 网页。 它需要查询字符串。 如果网站的名称为 WebSite1,URL http://localhost/WebSite1/RewritePath.aspx?page=1 将在浏览器中显示“第 1 页”。 网页后面的代码块是 Application_BeginRequest Global.asax 文件中的事件处理程序。 此代码截获 URL 的请求,例如 http://localhost/WebSite1/page1 ,并将其转换为在处理RewritePath.aspx之前所需的表单。 因此,URL http://localhost/WebSite1/page1 使用浏览器中显示“第 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 重写,可以透明地将请求转发到新页面位置。

如果希望使网站能够使用用户更友好且针对搜索引擎进行优化的 URL,更可靠的替代方法是使用 ASP.NET 路由。 有关详细信息,请参阅 ASP.NET 路由

另请参阅

适用于