Export (0) Print
Expand All
4 out of 4 rated this helpful - Rate this topic

HttpContext.RewritePath Method (String)

Rewrites the URL using the given path.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
public void RewritePath(
	string path
)

Parameters

path
Type: System.String

The internal rewrite path.

ExceptionCondition
ArgumentNullException

The path parameter is null.

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.

<%@ 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>
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.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.