Guest User

Untitled

a guest
Sep 26th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. <%@ WebHandler Language="VB" Class="webservice" %>
  2.  
  3. Imports System
  4. Imports System.Web
  5. Imports StoreModel
  6. Imports System.Web.Script.Serialization
  7.  
  8. Public Class product_for_api
  9. Private _name As String
  10. Public Property name() As String
  11. Get
  12. Return _name
  13. End Get
  14. Set(ByVal value As String)
  15. _name = value
  16. End Set
  17. End Property
  18.  
  19. Private _price As String
  20. Public Property price() As String
  21. Get
  22. Return _price
  23. End Get
  24. Set(ByVal value As String)
  25. _price = value
  26. End Set
  27. End Property
  28.  
  29. Private _description As String
  30. Public Property description() As String
  31. Get
  32. Return _description
  33. End Get
  34. Set(ByVal value As String)
  35. _description = value
  36. End Set
  37. End Property
  38.  
  39.  
  40.  
  41. End Class
  42.  
  43. Public Class webservice : Implements IHttpHandler
  44.  
  45. Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
  46.  
  47. 'instatiating javascript serializer
  48. Dim js_serializer As New JavaScriptSerializer()
  49. 'instatiating store entities context object
  50. Dim ctx As New StoreEntities
  51. 'retrieving product objects into a list of product objects
  52. Dim products As List(Of product) = (From p In ctx.products Order By p.name).ToList()
  53.  
  54. Dim products_for_api As New List(Of product_for_api)
  55. For Each product In products
  56. Dim product_for_api As New product_for_api
  57. product_for_api.name = product.name
  58. product_for_api.price = product.price
  59. product_for_api.description = product.description
  60. products_for_api.Add(product_for_api)
  61. Next
  62. 'converting list of product objects to a list of anonymous object with properties from product object,
  63. 'but only what we need for service to provide
  64. ' Dim products_for_json = products.Select(Function(p) New With {.name = p.name, .price = p.price}).ToList()
  65. 'using javascript serializer to serialize list of products for json into javascript object notation format
  66. 'and assigning it to as string variable
  67. Dim json_string As String = js_serializer.Serialize(products_for_api)
  68. 'writing json string to response stream
  69. context.Response.Write(json_string)
  70. context.Response.ContentType = "text/plain"
  71. End Sub
  72.  
  73. Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
  74. Get
  75. Return False
  76. End Get
  77. End Property
  78.  
  79. End Class
Add Comment
Please, Sign In to add comment