HttpContext.RewritePath 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将对资源的请求重定向到另一路径,使其不同于所请求 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
服务请求的资源的虚拟路径。
- queryString
- String
用于 URL 重定向的请求查询字符串。
- setClientFilePath
- Boolean
若要将用于客户端资源的文件路径设置为 filePath
参数的值,则为 true
;否则为 false
。
例外
path
参数不在当前应用程序的根目录中。
filePath
参数不在当前应用程序的根目录中。
示例
有关代码示例,请参阅 RewritePath(String) 方法重载。
注解
该 filePath
参数不包括参数的内容 pathInfo
。 对于 URL http://www.microsoft.com/virdir/page.html/tail
,参数 filePath
为 http://www.microsoft.com/virdir/page.html
,参数 pathInfo
为 tail。
若要确保不修改用于构造资源路径的虚拟路径,请将 setClientFilePath
参数设置为 false
。 可能需要设置 setClientFilePath
为 false
的一种常见方案是需要重写 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
内部重写路径。
- queryString
- String
请求查询字符串。
例外
path
参数不在当前应用程序的根目录中。
filePath
参数不在当前应用程序的根目录中。
示例
有关代码示例,包括此方法重载的示例,请参阅 RewritePath(String) 方法重载。
注解
该方法 RewritePath 在不更改 URL 的情况下将资源请求重定向到另一个资源。
该 filePath
参数不包括 pathInfo
参数内容。 对于 URL http://www.microsoft.com/virdir/page.html/tail
,参数 filePath
为 http://www.microsoft.com/virdir/page.html
,参数 pathInfo
为 tail。
如果要在 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
。
例外
path
参数为 null
。
path
参数不在当前应用程序的根目录中。
示例
有关代码示例,请参阅 RewritePath(String) 方法重载。
注解
该方法HttpContext.RewritePath(String, Boolean)由HttpContext.RewritePath(String)参数设置为 true
的方法rebaseClientPath
调用。 若要确保用于构造资源路径的虚拟路径未修改,请将 rebaseClientPath
参数设置为 false
。 可能需要将 URL 设置为的rebaseClientPath``false
常见方案是需要重写 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
内部重写路径。
例外
path
参数为 null
。
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
,并将其转换为在处理重写Path.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 路由。