练习 2: 创建一个GeoRSS 订阅源
在本练习中,您将在ASP.NET Web应用程序当中实现一个HTTP Handler,并用它来返回一个GeoRSS订阅源。GeoRSS 是一种包含地理信息数据的RSS订阅标准格式,它定义一个特定的格式,叫做GeoRSS GML,用来在订阅中包含GML格式的数据。客户端应用程序可以以普通的RSS源的方式来订阅GeoRSS源。而以GeoRSS格式返回的数据则可以被轻松的导入到Microsoft Virtual Earth VEMap控件当中。
实现一个HTTP Handler
启用 Microsoft Visual Studio 2008。
打开File 菜单,点击Open,点击Web Site,然后打开C:\SQLHOLs\Spatial and VE\Starter\StoreFinderSite Web 站点。
在Solution Explorer中,展开App_Code目录,然后双击GeoRSSHandler.vb 在代码编辑器中打开它。
注意: HTTP Handler 是一个代码模块,它可以处理到Web应用程序的HTTP 请求。通常,到一个Web应用程序的HTTP请求是由一些默认的ASP.NET 请求处理器所处理的,但是您可以为特定的扩展名创建自定义的处理器。这时,您将实现一个请求处理器,它将会返回.georss为扩展名为的处理结果。
查看现有的代码。用来处理请求的函数叫做ProcessRequest。注意,这段代码还没有完成,有些代码需要继续填充完整。
在Build the GeoRSS feed注释下面,添加下面代码开始构建由HTTP Handler返回的GeoRSS 源。
rssOutput.AppendLine("xmlns:georss='http://www.georss.org/georss'")
rssOutput.AppendLine("xmlns:gml='http://www.opengis.net/gml'>")
rssOutput.AppendLine("<title>Stores</title>")
rssOutput.AppendLine("<subtitle>Store Locations</subtitle>")
rssOutput.AppendLine("<link href='http://localhost/storefindersite/'/>")
rssOutput.AppendLine("<updated>" + System.DateTime.Now + "</updated>")
rssOutput.AppendLine("<author>")
rssOutput.AppendLine("<name>SQL Server</name>")
rssOutput.AppendLine("</author>")
在Open a connection to the database注释下面,添加下面的代码。
sqlConn.Open()
在Use the GetStoresGML stored proc to get all stores by default注释下面,添加下面的代码。
spName = "GetStoresGML"
注意: 默认情况下, 到这个HTTP Handler的请求处理将会调用GetStoresGML 存储过程,并将返回包所有商店的GeoRSS 源。
在 If a searchFrom parameter is provided, use GetNearbyStores and add the provided lat and lon coordinates as parameters注释下面,添加下面的代码。
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
注意: 如果请求包含一个参数,叫做SearchFrom(它将会包含一个用逗号分隔的经度和纬度的对),处理器将会从参数中提取其中的经度和纬度值,并使用GetNearbyStoresGML 存储过程返回一个GeoRSS源,它将包含请求搜索点100公里附近的商店。
在Specify the stored procedure name as the command text注释下面,添加下面的代码。
cmd.CommandText = spName
在Create an <entry> element for this row注释下面,添加下面的代码,来为存储过程返回的结果的每行数据创建一个<entry> 标签。
rssOutput.AppendLine("<entry>")
在Use columns 0 and 1 for the title and description注释下面,添加下面的代码来为存储过程返回的数据创建<title> 和<description> 元素。
rssOutput.AppendLine(String.Format("<description>{0}</description>", _
geomRdr.GetValue(1)))
在Add a <georss:where> element注释下面,添加下面的代码来为这个GML数据节点创建一个<where> 元素。
rssOutput.AppendLine("<georss:where>")
在Get the geography instance GML from column 2注释下面,添加下面的代码,来从存储过程结果中获取GML数据。
gml = geomRdr.GetValue(2).ToString()
在Add the <gml:> elements to the output XML注释下面,添加下面的代码,来向GeoRSS 源添加GML数据。
rssOutput.AppendLine(gml)
在Close <georss:where> and <entry> elements注释下面,添加下面的代码。
rssOutput.AppendLine("</georss:where>")
rssOutput.AppendLine("</entry>")
在Close the <feed> document and send it as the response注释下面,添加下面的代码来完成GeoRSS 源,并将它发送回请求方。
rssOutput.Append("</feed>")
context.Response.Write(rssOutput.ToString())
保存 GeoRSSHandler.vb。
注册HTTP Handler
在 Solution Explorer中,双击web.config ,在编辑器中打开它。
在<httpHandlers> 节点中,在Register the GeoRSSHandler for .georss requests注释下面,添加下面的XML代码。
<add verb="*" path="*.georss" type="GeoRSSHandler" validate="false"/>
注意: 您必须注册这个HTTP Handlers,用来指定某个特定的文件扩展名,从而让Internet Information Services 可以将这种特定的请求发送给适当的处理器来处理这种文件。
保存 web.config。
添加HTTP Handler
在 Solution Explorer中,点击位于树状图根结点的Web 站点项目文件然后点击F4 查看它的属性。
注意Port number 属性。
在Website 菜单中,点击Start Options。
选择Start URL,输入下面的URL (将<port> 修改为Web站点的Port number 属性),然后点击OK。
http://localhost:<port>/storefindersite/test.georss
在Debug 菜单中,点击Start Debugging。
当 Microsoft Internet Explorer 打开后,查看包含商店名称的RSS源的页面。
在Internet Explorer中,在Web 页面中,右键单击任意位置,然后点击View Source 在Notepad中查看页面的源代码。注意,页面的源是由HTTP Handler所生成的GeoRSS 源。
关闭 Notepad。
在 Internet Explorer中,在地址栏(Address bar)中,在上面的网址后面输入下面的查询字符串,点击键盘上的回车键:
?SearchFrom=34.000000,-118.000000
确认返回的GeoRSS源中包含了一个搜索区域,以及其中的任意商店。
关闭Internet Explorer。
保持Visual Studio 打开,以备下个练习使用。