Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Don't Like the URL? Change It


Don’t like the URL? Change It


Many web sites serve dynamically generated pages—pages that of course don’t yet exist at the time the application is deployed. For example, a web site that offers daily news will easily reference pages using a fixed URL and a progressive ID in the query string as follows: view.aspx?ID=1023.

However, similar-looking URLs don't necessarily all point to the same physical page, view.aspx. In some cases, the displayed URL is dynamically mapped to a real ASPX page with a different name.

Can you change the URL of the requested page on the fly? If you ask this question to a sample of ASP.NET developers, the average answer is Maybe, resulting from a balanced set of Yes and No.

Can you redirect the browser to show another page? If you ask this question, instead, the answer is a resounding Yes. In both cases, you change the URL of the originally requested page and actually force the browser to display a different page. However, URL change and classic redirect work in two radically different ways.

Changing the URL is an operation that is technically known as "URL rewriting. It takes place during the request processing and determines the overwriting of the originally requested URL. The ASP.NET HTTP context object has a method named RewritePath defined as follows:

public void RewritePath(string path)

The RewritePath method issues a call to an internal method defined by the HttpRequest object. This method accesses some private fields on the Request object and rewrites the target URL of the request. As a result, the displayed page is the one you set through RewritePath; the page shown in the address bar remains the originally requested one. The change of the final URL takes place on the server and, more importantly, within the context of the same call.

This fact represents the biggest difference with the classic browser redirection. When you call Response.Redirect, in fact, the browser receives an HTTP 302 status code and reiterates the request addressing a different URL.

All in all, RewritePath is more efficient than classic redirection because it doesn't require a second call to the browser. However, RewritePath should be used carefully and mainly from within the global.asax file. If you use RewritePath in the context of a postback event, you might experience some viewstate troubles. The following code shows how to rewrite the URL of a request to point to a different page:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
   HttpContext context = HttpContext.Current;
   context.RewritePath("next.aspx");
}

The code is an excerpt from a web application's global.asax class file.

It is worth noticing that RewritePath is used internally to ASP.NET to implement the cookieless session feature. When cookieless sessions are configured, each request embodies a fake URL that includes the session ID. For the successful execution of the request, that fake URL must be replaced with a real one and possibly without performance penalties. That's where HttpContext.RewritePath fits in.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.