In this exercise, you will implement an HTTP handler in an ASP.NET web application to return a GeoRSS feed. GeoRSS is a standard for including geospatial data in RSS feeds. It defines a specific format called GeoRSS GML for including GML-formatted data in feeds. Client applications can subscribe to GeoRSS feeds in the same way as regular RSS feeds. Data in GeoRSS format can be easily imported into the Microsoft Virtual Earth VEMap control.
Note: You can copy the code used in this exercise from the completed website page in C:SQLHOLSSpatial and VESolutionStoreFinderSite.
Implement HTTP handlers
1. Start Microsoft Visual Studio 2008.
2. On the File menu, click Open Website, and then open the C:SQLHOLsSpatial and VEStarterStoreFinderSite website.
3. In Solution Explorer, expand App_Code, and then double-click GeoRSSHandler.vb to open it in the code editor.
Note: An HTTP handler is a code module that handles HTTP requests to a web application. Requests to ASP.NET Web applications are typically handled by the default ASP.NET request handler, but you can create custom handlers for specific file extensions. In this example, you will implement a handler that will handle requests for files with a .georss extension.
4. Check the existing code. The process that handles incoming requests is called ProcessRequest. Please note that this procedure is incomplete and contains extensive comments that must be added to the code.
5. Under the comment Build the GeoRSS feed, add the following code to start building the GeoRSS feed that will be returned by the HTTP handler.
rssOutput.AppendLine(" rssOutput.AppendLine("xmlns:georss='http://www.georss.org/georss'") rssOutput.AppendLine("xmlns:gml='http://www.opengis.net/gml '>") rssOutput.AppendLine("") rssOutput.AppendLine("Store Locations") rssOutput.AppendLine("") rssOutput.AppendLine("" + System.DateTime.Now + "") rssOutput.AppendLine("") rssOutput.AppendLine("SQL Server") rssOutput.AppendLine("")
6. Under the comment Open a connection to the database, add the following code.
sqlConn.Open()
7. Under the comment Use the GetStoresGML stored proc to get all stores by default, add the following code.
spName = "GetStoresGML"
Note: By default, requests to this HTTP handler call the GetStoresGML stored procedure and return a GeoRSS feed containing all stores.
8. Under the comment If a searchFrom parameter is provided, use GetNearbyStores and add the provided lat and lon coordinates as parameters, add the following code.
Dim searchFrom As String = context.Request.QueryString("SearchFrom") If Not searchFrom Is Nothing Then spName = "GetNearbyStoresGML" Dim latLong() As String = Split(searchFrom, ",", 2) cmd.Parameters.Add(New SqlParameter("Lat", latLong(0))) cmd.Parameters.Add(New SqlParameter("Long", latLong(1))) End If
Note: If the request contains a parameter named SearchFrom (assuming it contains a comma-separated pair of latitude and longitude coordinates), the handler will extract the latitude and longitude values from this parameter and use the GetNearbyStoresGML stored procedure to return a GeoRSS feed containing Shops within a 100 km radius of the requested search point.
9. Under the comment Specify the stored procedure name as the command text, add the following code.
cmd.CommandText = spName
10. Under the comment Create an element for this row, add the following code to create a tag for each row in the result of the stored procedure.
rssOutput.AppendLine("")
11. Under the comment Use columns 0 and 1 for the title and description, add the following code to create ", geomRdr.GetValue(0))) based on the data returned by the stored procedure.
rssOutput.AppendLine(String.Format("{0}", _ geomRdr.GetValue(1)))
12. Under the comment Add a element, add the following code to create an element for this entry.
rssOutput.AppendLine("")
13. Under the comment Get the geography instance GML from column 2, add the following code to retrieve the GML data from the stored procedure result.
gml = geomRdr.GetValue(2).ToString()
14. Under the comment Add the elements to the output XML, add the following code to add GML data to the GeoRSS feed.
rssOutput.AppendLine(gml)
15. Under the comment Close and elements, add the following code.
rssOutput.AppendLine("") rssOutput.AppendLine("")
16. Under the comment Close the document and send it as the response, add the following code to complete the GeoRSS feed and send it to the requester.
rssOutput.Append("") context.Response.Write(rssOutput.ToString())
17. Save GeoRSSHandler.vb.
Register HTTP handler
1. In Solution Explorer, double-click web.config to open it in the editor.
2. In the section, under the comment Register the GeoRSSHandler for .georss requests, add the following XML.
<add verb="*" path="*.georss" type="GeoRSSHandler" validate="false"/>
Note: You must register HTTP handlers for specific file extensions so that Internet Information Services forwards requests for these files to the correct handler.
3. Save web.config.
Test HTTP handler
1. In Solution Explorer, click the website project file located at the root of the tree, and then press F4 to view its properties.
2. Please pay attention to the port number attribute.
3. On the Site menu, click Launch Options.
4. Select Launch URL, enter the following URL (replace port with the value of the port number attribute of the website), and click OK.
http://localhost:/storefindersite/test.georss
5. On the Debug menu, click Start execution without debugging.
6. When Microsoft Internet Explorer ® opens, view the page containing the RSS feed for the store name.
7. In Internet Explorer, right-click anywhere on the web page, and then click View Source to open the source file for the page in Notepad. Note that the source of this page is the GeoRSS feed generated by the HTTP handler you created earlier.
8. Close Notepad.
9. In the address bar in Internet Explorer, append the following query string to the URL and press Enter.
?SearchFrom=34.000000,-118.000000
10. Verify that the generated GeoRSS feed contains the search area and all stores in it.
11. Close Internet Explorer