Home   Preview the Book   Chapter 15, TOC   Part 30

  Previous: Inviting Spiders, Crawlers, and Surfers
  Next: Adding Meta-Keywords Dynamically

Creating Web Pages on the Fly

What does this have to do with the names of your site’s Web pages? Consider how the spiders and Web crawlers used by many search engines work: they happily follow link after link after link, recording information about the Web site’s pages as they go. However, they most definitely do not make up values to enter into textboxes to determine how a Web site responds, and they don’t register themselves to view data that is unavailable to the general public. In fact, some search engines immediately stop crawling a site when they encounter a “?” in a URL’s query string to avoid the possibility of getting caught in a “spider trap”—a bit of dynamic code that requests information that the spider can’t supply.

To help the spiders along, you want to make sure that every page in your Web site has its own URL, even if the page does not exist until you create it. Furthermore, you want to make sure that your Web site has several ways to link to every accessible page. By creating multiple links to the content-rich pages, you increase the odds that search engines will follow the links and find the most relevant pages of your Web site. In the VB Snippets case study, for example, many links exist to each code snippet.

So that explains why every code example and every browse page in VB Snippets has a different URL. The code in Listing 15-14 shows how VB Snippets analyzes the URLs and routes incoming requests to the code that creates those pages on the fly. The code that accomplishes this resides in the Global.asax file and executes before an ASPX file has been designated to respond to an incoming request. The Application_BeginRequest event handler contains the required code.

Listing 15-14. The Application_BeginRequest Event Handler Routes Incoming Requests for Code Examples and Browse Pages.

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires at the beginning of each request
    Const sCodeReq As String = "code_example_"
    Const sBrowse As String = "code_browse_"
    Dim sPath As String = Request.Path.ToLower
    Dim iLength As Integer = 0

    Dim iFirstDigit As Integer = sPath.IndexOf(sCodeReq)
    If iFirstDigit > -1 Then
        'Code example page name detected
        iFirstDigit += sCodeReq.Length
        iLength = sPath.IndexOf(".") - iFirstDigit
        If iLength > 0 Then
            'Code example request: convert request to parameters and create page
            Try
                'Find language directory, if any
                Dim asLang() As String = {"/es/", "/fr/", "/pt/", "/de/"}
                Try
                    asLang _
                      = ConfigurationSettings.AppSettings("UrlLangs").Split(","c)
                Catch
                    'Retain default
                End Try

                Dim sLang As String = String.Empty
                Dim i, iSlash As Integer
                For i = 0 To UBound(asLang)
                    iSlash = sPath.IndexOf(asLang(i))
                    If iSlash > -1 Then
                        sLang = asLang(i).Substring(1, asLang(i).Length - 2)
                        Exit For
                    End If
                Next

                Dim iExampleID As Integer _
                    = CType(sPath.Substring(iFirstDigit, iLength), Integer)
                Dim strNewPath As String = Request.ApplicationPath _
                    & "/code_example.aspx?exampleid=" & iExampleID.ToString
                If sLang <> String.Empty Then
                    strNewPath &= "&language=" & sLang
                End If

                Me.Context.RewritePath(strNewPath)
            Catch
                'On conversion error, do not rewrite path
            End Try
            Exit Sub
        End If
    End If

    Dim iFirstChar As Integer = sPath.IndexOf(sBrowse)
    If iFirstChar > -1 Then
        'Browse page detected
        iFirstChar += sBrowse.Length
        iLength = sPath.IndexOf(".") - iFirstChar
        Dim sKeyword As String = sPath.Substring(iFirstChar, iLength)
        If sKeyword <> "all" Then
            'Specific keyword browse, so create page
            Me.Context.RewritePath(Request.ApplicationPath _
                & "/code_browse.aspx?keyword=" & sKeyword)
        End If
    End If
End Sub

In a nutshell, the code in Listing 15-14 scans the URLs of incoming requests to determine if the URL includes “code_example_” or “code_browse_”. If not, the code does nothing and the requested page—index.aspx, for example—responds as usual. If a match is found, however, the event handler goes to work.

When the URL is for a particular code example, the event handler finds the example number, which corresponds to its ID in the VbCode database, and inserts into a query string like this:

?exampleid=13

The event handler uses the Context.RewritePath method to route the incoming request to code_example.aspx, with the query string appended. The code_example page uses the query string to determine what example has been requested and builds the response on the fly. When ASP.NET returns the response to the visitor, however, the URL in the browser’s address line reflects the original request, not the page that actually created the response. To all appearances, the visitor requested a Web page with an independent existence, and the Web site returned that page.

If the request is for a code example with a non-English description, the URL includes the culture code, like this:

.../es/code_example_13.aspx

When the URL contains a recognized culture code, the event handler extracts it and sends it to code_example.aspx in a separate query string.

The code_browse processing works similarly. In this case, however, the event handler extracts the keyword from the URL and passes it to code_browse via a query string.

The Request object that’s available to your Application_BeginRender code provides access to specific information about the visitor’s browser. If your Web site provides different versions of Web pages for different browsers, your code can use the Context.RewritePath method to route an incoming request to the page specifically designed to handle it. From the visitor’s viewpoint, the Web application simply returns the page requested, regardless of the ASPX file that actually responded to the request.

  Previous: Inviting Spiders, Crawlers, and Surfers
  Next: Adding Meta-Keywords Dynamically

Home   Preview the Book   Chapter 15, TOC   Part 30

Copyright © 1996 - 2017 SoftMedia Artisans, Inc. All Rights Reserved.