Muokkaa

Jaa


HttpContext.RewritePath Method

Definition

Redirects a request for a resource to a different path than the one that is indicated by the requested URL. RewritePath is used in cookieless session state to strip session IDs from URLs.

Overloads

RewritePath(String, String, String, Boolean)

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)

Rewrites the URL by using the given path, path information, and query string information.

RewritePath(String, Boolean)

Rewrites the URL using the given path and a Boolean value that specifies whether the virtual path for server resources is modified.

RewritePath(String)

Rewrites the URL using the given path.

RewritePath(String, String, String, Boolean)

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)

Parameters

filePath
String

The virtual path to the resource that services the request.

pathInfo
String

Additional path information to use for the URL redirect. For more information, see PathInfo.

queryString
String

The request query string to use for the URL redirect.

setClientFilePath
Boolean

true to set the file path used for client resources to the value of the filePath parameter; otherwise false.

Exceptions

The path parameter is not in the current application's root directory.

The filePath parameter is not in the current application's root directory.

Examples

For a code example, see the RewritePath(String) method overload.

Remarks

The filePath parameter does not include the content of the pathInfo parameter. 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.

To ensure that the virtual path that is used to construct paths to resources is not modified, set the setClientFilePath parameter to false. 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.

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 rewriting enables you to transparently forward requests to the new page location.

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. For more information, see ASP.NET Routing.

See also

Applies to

RewritePath(String, String, String)

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)

Parameters

filePath
String

The internal rewrite path.

pathInfo
String

Additional path information for a resource. For more information, see PathInfo.

queryString
String

The request query string.

Exceptions

The path parameter is not in the current application's root directory.

The filePath parameter is not in the current application's root directory.

Examples

For a code example, including an example of this method overload, see the RewritePath(String) method overload.

Remarks

The RewritePath method redirects a request for a resource to another resource without changing the URL.

The filePath parameter does not include the pathInfo parameter content. 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.

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 rewriting enables you to transparently forward requests to the new page location.

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. For more information, see ASP.NET Routing.

See also

Applies to

RewritePath(String, Boolean)

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)

Parameters

path
String

The internal rewrite path.

rebaseClientPath
Boolean

true to reset the virtual path; false to keep the virtual path unchanged.

Exceptions

The path parameter is null.

The path parameter is not in the current application's root directory.

Examples

For a code example, see the RewritePath(String) method overload.

Remarks

The HttpContext.RewritePath(String, Boolean) method is called by the HttpContext.RewritePath(String) method with the rebaseClientPath parameter set to true. To ensure that the virtual path that is used to construct paths to resources is not modified, set the rebaseClientPath parameter to false. 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.

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 rewriting enables you to transparently forward requests to the new page location.

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. For more information, see ASP.NET Routing.

See also

Applies to

RewritePath(String)

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)

Parameters

path
String

The internal rewrite path.

Exceptions

The path parameter is null.

The path parameter is not in the current application's root directory.

Examples

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. The first block of code is an ASP.NET Web page that is named RewritePath.aspx. It requires a query string. If the name of your site is WebSite1, the URL http://localhost/WebSite1/RewritePath.aspx?page=1 displays "Page 1" in the browser. The block of code that follows the Web page is the Application_BeginRequest event handler in the Global.asax file. 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. Therefore, the URL http://localhost/WebSite1/page1 invokes RewritePath.aspx with the query-string parameter that displays "Page 1" in the browser. 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

Remarks

The RewritePath(String) method redirects a request for a resource to a different path than the one that is indicated by the requested URL. 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.

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 rewriting enables you to transparently forward requests to the new page location.

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. For more information, see ASP.NET Routing.

See also

Applies to