WebApplication.HandleURL
From Xojo Documentation
This event is only available for Web applications. |
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
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".
Starting with 2018r1, the Date header is added to the incoming request.
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.
Request.Print "<html><body>Hello World!</body></html>"
Request.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:
If Request.Path <> "" Then
// First, create the HTML answer
Request.Print "<html><body>"
Request.Print "Oops! You took a wrong turn. "
Request.Print "Please put it in reverse and try again."
Request.Print "</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.
Request.Status = 200
// You MUST return True if you want to override the output.
Return True
End If
See Also
WebRequest, WebSession, WebSessionContext; UserGuide:Eddie's Electronics Sample Web Service topic