Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
System.Web
HttpContext Class
HttpContext Methods
 RewritePath Method (String)
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
HttpContext..::.RewritePath Method (String)

Rewrites the URL using the given path.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
Visual Basic
Public Sub RewritePath ( _
    path As String _
)
C#
public void RewritePath(
    string path
)
Visual C++
public:
void RewritePath(
    String^ path
)
F#
member RewritePath : 
        path:string -> unit 

Parameters

path
Type: System..::.String
The internal rewrite path.
ExceptionCondition
ArgumentNullException

The path parameter is nullNothingnullptra null reference (Nothing in Visual Basic).

HttpException

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

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.

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.

Visual Basic
<%@ 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 >
<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>
C#
<%@ 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 >
<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>
Visual Basic
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
C#
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");
    }
}    

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker