WebApplication.HandleURL

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Event


WebApplication.HandleURL(Request As WebRequest, Response As WebResponse) As Boolean

New in 2014r3

Supported for all project types and targets.

This event fires when an http request comes to your app which would otherwise result in a 404 Page Missing response.

Notes

If your code has handled this event, return True and False if you did not. You can also return False to prevent a Session from starting.

To call a web service implemented using HandlSpecialURL you would use an address similar to this:

http://127.0.0.1:8080/GetAllCustomers

The above address is to a standalone web app running on the local machine. In this method, Request.Path contains "GetAllCustomers".

To override the HTTP 404 error response, use Request.Print to output the data you wish to send, set the Request.Status to the HTTP status code you wish to return and return True.

If you are building a standalone web app, you will need to check Request.Path to see if it is empty. An empty string means that a browser has connected to the root level of your app.

If Request.Path <> "" then
Response.Write("<html><body>Hello World!</body></html>")
Response.Status = 200 // An HTTP Response code of 200 means everything is OK
Return True
End If

You can use this to essentially serve data from just about any URL that you want. There are a few exceptions for things that are reserved and will not call this event:

  • /framework - This is for framework files
  • /xojo - for future development
  • _ (anything that starts with an underscore at the top level)
  • Session URLs - these are the hex encoded session IDs which start with /B156D2E6338304E2AAD151DC5680F50E
  • /api and /special - URLs which would have gone to WebApplication.HandleSpecialURL. This is so existing apps continue to work as expected.

Indexing for Google

With the HandleURL method, you can now create pages that Google can index. You return valid HTML for Google, including links for "Next" and "Previous" pages.

Sample Code

Return a custom 404 page:

// Don't override an empty request, or a session won't start.
If Request.Path <> "" Then
// First, create the HTML answer
Response.Write "<html><body>"
Response.Write "Oops! You took a wrong turn. "
Response.Write "Please put it in reverse and try again."
Response.Write "</body></html>"

// Set the status code. The default value is 404,
// but if you leave it that way, some browsers
// will show their own. A value of 200 will make
// the browser show your text instead.
Response.Status = 200

// You MUST return True if you want to override the output.
Return True
End If

See Also

WebRequest, WebResponse, WebSession, WebSessionContext; UserGuide:Eddie's Electronics Sample Web Service topic