在ASP.NET Web应用中创建GeoRSS订阅源


在此练习中,您将在 ASP.NET Web 应用程序中实现 HTTP 处理程序以返回 GeoRSS 订阅源。GeoRSS 是在 RSS 订阅源中包含地理空间数据时所用的一个标准,它定义了一种名为 GeoRSS GML 的特定格式,用来在订阅源中包含 GML 格式的数据。客户端应用程序可以订阅 GeoRSS 订阅源,订阅方式与订阅常规 RSS 订阅源相同。可以轻松地将 GeoRSS 格式的数据导入 Microsoft Virtual Earth VEMap 控件中。

  注意:您可以从 C:\SQLHOLS\Spatial and VE\Solution\StoreFinderSite 中的完成的网站页面复制此练习中所用的代码。

  实现 HTTP 处理程序

  1. 启动 Microsoft Visual Studio 2008。

  2. 在文件菜单中,单击打开网站,然后打开 C:\SQLHOLs\Spatial and VE\Starter\StoreFinderSite 网站。

  3. 在解决方案资源管理器中,展开 App_Code,然后双击 GeoRSSHandler.vb 在代码编辑器中打开它。

  注意:HTTP 处理程序是一个代码模块,用于处理对 Web 应用程序的 HTTP 请求。通常由默认 ASP.NET 请求处理程序处理对 ASP.NET Web 应用程序的请求,但是您可以针对特定文件扩展名创建自定义处理程序。在本例中,您将实现一个将用于处理扩展名为 .georss 的文件的请求的处理程序。

  4. 检查现有的代码。处理传入请求的过程名为 ProcessRequest。请注意,此过程不完整,包含大量必须添加代码的注释。

  5. 在注释 Build the GeoRSS feed下,添加以下代码,以开始构建将由 HTTP 处理程序返回的 GeoRSS 订阅源。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  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. 在注释 Open a connection to the database下,添加以下代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  sqlConn.Open()
   7. 在注释 Use the GetStoresGML stored proc to get all stores by default下,添加以下代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  spName = "GetStoresGML"
  注意:默认情况下,对此 HTTP 处理程序的请求将调用 GetStoresGML 存储过程,并返回包含所有商店的 GeoRSS 订阅源。

  8. 在注释If a searchFrom parameter is provided, use GetNearbyStores and add the provided lat and lon coordinates as parameters下,添加以下代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  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
   注意:如果请求包含名为 SearchFrom 的参数(假定它包含以逗号分隔的纬度和经度坐标对),处理程序将从此参数提取纬度和经度值,并使用 GetNearbyStoresGML 存储过程返回 GeoRSS 订阅源,订阅源中包含请求的搜索点周围方圆 100 km 范围内的商店。

  9. 在注释 Specify the stored procedure name as the command text(将存储过程名称指定为命令文本)下,添加以下代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  cmd.CommandText = spName
   10. 在注释 Create an element for this row下,添加以下代码来为存储过程的结果中的每一行都创建一个 标记。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  rssOutput.AppendLine("")
  11. 在注释 Use columns 0 and 1 for the title and description下,添加以下代码以根据存储过程返回的数据创建 ", geomRdr.GetValue(0)))


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  rssOutput.AppendLine(String.Format("{0}", _

  geomRdr.GetValue(1)))
  12. 在注释 Add a element下,添加以下代码来为此条目创建 元素。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  rssOutput.AppendLine("")
  13. 在注释 Get the geography instance GML from column 2下,添加以下代码,以从存储过程结果中检索 GML 数据。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  gml = geomRdr.GetValue(2).ToString()
   14. 在注释 Add the elements to the output XML下,添加以下代码以向 GeoRSS 订阅源添加 GML 数据。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  rssOutput.AppendLine(gml)
   15. 在注释 Close and elements下,添加以下代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  rssOutput.AppendLine("")

  rssOutput.AppendLine("")
   16. 在注释 Close the document and send it as the response下,添加以下代码以完成 GeoRSS 订阅源并将其发送给请求人。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  rssOutput.Append("")

  context.Response.Write(rssOutput.ToString())
   17. 保存 GeoRSSHandler.vb。

  注册 HTTP 处理程序

  1. 在解决方案资源管理器中,双击 web.config 在编辑器中打开它。

  2. 在 部分中,在注释 Register the GeoRSSHandler for .georss requests下,添加以下 XML。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><add verb="*" path="*.georss" type="GeoRSSHandler" validate="false"/>
   注意:您必须为特定文件扩展名注册 HTTP 处理程序,以便 Internet Information Services 将针对这些文件的请求转发到正确的处理程序。

  3. 保存 web.config。

  测试 HTTP 处理程序

  1. 在解决方案资源管理器中,单击位于树的根目录下的网站项目文件,然后按 F4 查看其属性。

  2. 请注意观察端口号属性。

  3. 在网站菜单上,单击启动选项。

  4. 选择启动 URL,输入以下 URL(将 port 替换为网站的端口号属性的值),然后单击确定。

  http://localhost:/storefindersite/test.georss

  5. 在调试菜单上,单击开始执行(不调试)。

  6. 当 Microsoft Internet Explorer ? 打开时,查看包含商店名称的 RSS 订阅源的页面。

  7. 在 Internet Explorer 中,右键单击该网页的任意位置,然后单击查看源文件以在记事本中打开该页的源文件。请注意,该页的源是您前面创建的 HTTP 处理程序生成的 GeoRSS 订阅源。

  8. 关闭记事本。

  9. 在 Internet Explorer 中的地址栏中,将以下查询字符串附加到 URL 后,然后按 Enter。

  ?SearchFrom=34.000000,-118.000000

  10. 验证生成的 GeoRSS 订阅源包含搜索区域及其中的所有商店。

  11. 关闭 Internet Explorer


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3