Sign In
How to dynamically redirect a Web page to its referring page 

Download Sample Project

Softcoding URLs is always a good thing to do in Web applications. For example, imagine a Web form that updates a customer record. Ideally, this form should return to the URL that called it, not just a single, hardcoded URL. This article shows how to fetch the incoming URL for a page so that you can later redirect back to that URL.

The referring URL is available through the UrlReferrer property of the page's Request object (which is an instance of the HttpRequest class). Fetching it looks like this:

    DclFld ReferringUrl Type( *String ) 

    ReferringUrl = Request.UrlReferrer.ToString()

There are three important things to consider beyond simply fetching this value.

1. The value returned includes the query string used to call the referring page

Assume Page2.aspx redirects to Page3.aspx and you want Page3.aspx to dynamically redirect back to Page2.aspx. If Page2.aspx was originally invoked with a query string, the URL that Request.UrlReferrer returns includes that query string.

For example, let's assume that Page1.aspx redirected to Page2.aspx with this redirect:

    Response.Redirect( "page2.aspx?rank=1&exempt=no" ) 

when Page2.aspx redirects to Page3.aspx, the UrlReferring property. when checked in Page3.aspx, includes the query string originally used to invoke Page2.aspx. In almost every case, you'll want to remove that query string from the UrlReferrer before redirecting back to the page it specifies.

2. You'll want to fetch and save the referring URL immediately

Each postback changes the Referring URL. Therefore it's important to save the incoming Referring URL in the NOT Page.IsPostBack and save it (probably in a Viewstate variable) for use when it's needed.

3. Don't assume that Request.UrlReferrer has a value

If you attempt to fetch the Request.UrlReferrer on the first page of the application, it will be *Nothing. Pages referred to from the address bar in the browser don't populate the Request.UrlReferrer property.

Keeping these three issues in mind, here is the code to dynamically redirect to calling page:

BegSr Page_Load Access(*Private) Event(*This.Load)
    DclSrParm sender Type( *Object)
    DclSrParm e Type(System.EventArgs)

    If (NOT Page.IsPostBack)
        ViewState[ "CallerUrl" ] = GetReferringUrl()

The code above uses a function called GetReferringUrl() to save the referring URL to a session variable:

The follolwing code redirects back to the referring URL:

BegSr MyRoutine
    // Redirect back to the calling page.
    If ( ViewState[ "CallerUrl" ].ToString().ToUpper() <> "*HOME )
        Response.Redirect( ViewState[ "CallerUrl" ].ToString() )

Note that the code above tests for the special value of "*HOME." You'll see in a moment that GetReferringUrl() sets this value if Request.UrlReferrer is *Nothing. As a general rule, you'll rarely have to worry about this. Generally you wouldn't ever have the logic on the home page (the first page, which is the only page where Request.UrlReferrer can be *Nothing) to redirect back to a caller. But at least the test is there if you need it (as you might if you put this GetReferringUrl() in a base page).

Here is the GetReferringUrl() function to actually fetch the referring URL. It uses the following line of code to trim off the query string. This line is using the RegEx class's replace method to replace a question mark and any characters that follow it with an empty string (effectively removing the question mark and any characters that follow).

        BaseUrl = RegEx.Replace( ReferringUrl, "\?.*$", String.Empty ) 

This code is using the "\?.*$" regular expression which translates to:

Expression Description
\? Find a question mark. Because a question mark is a special regular expression metacharacter, it must be escaped with the slash.
.* The period is the regular expression metacharacter for "any character." The asterisk specifies zero or more of the previous character. In other words, these two characters match any characters following the question mark.
$ The dollar sign is the regular expression metacharacter for anchoring the previous search to the end of the line.

If the referring URL is *Nothing, the special value "*Home" is returned.

BegFunc GetReferringUrl Type( *String )
    DclFld ReferringURL Type( *String )

    // This routine removes any existing query string from the referring
    // URL. For example, if the request is
    // or
    // or
    // the value "" is returned.
    // Requires: Using System.Text.RegularExpressions

    DclFld BaseUrl Type( *String )
    DclFld ReferringUrl Type( *String )
    DclFld m Type( Match )

    // Get the full referring URL.
    If ( Request.UrlReferrer <> *Nothing )
        ReferringUrl = Request.UrlReferrer.ToString()
        BaseUrl = RegEx.Replace( ReferringUrl, "\?.*$", String.Empty )
        BaseUrl = "*Home"

    LeaveSr BaseUrl

Because GetReferringUrl returns the URL without a query string, you might need to assembly a new one (perhaps using different values) before you redirect to that page. If you needed to have the query string left intact, you could modify GetReferringUrl to conditionally remove the query string (or just reference Request.UrlReferrer directly). Also, in the real world, you'll probably want to package GetReferringUrl into a class library of frequently-used functions.

As you can see, with just a little effort, it's easy to avoid hard-coding redirect pages. Using this technique makes your Web applications much more flexible.

Related Articles
Article Downloads
Article ID: 401 
Category: ASNA Visual RPG : Web Development 
Applies To: AVR 8.0 
Article Date: 9/18/2008