Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ WebHandler Language="VB" Class="webservice" %>
- Imports System
- Imports System.Web
- Imports StoreModel
- Imports System.Web.Script.Serialization
- Public Class product_for_api
- Private _name As String
- Public Property name() As String
- Get
- Return _name
- End Get
- Set(ByVal value As String)
- _name = value
- End Set
- End Property
- Private _price As String
- Public Property price() As String
- Get
- Return _price
- End Get
- Set(ByVal value As String)
- _price = value
- End Set
- End Property
- Private _description As String
- Public Property description() As String
- Get
- Return _description
- End Get
- Set(ByVal value As String)
- _description = value
- End Set
- End Property
- End Class
- Public Class webservice : Implements IHttpHandler
- Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
- 'instatiating javascript serializer
- Dim js_serializer As New JavaScriptSerializer()
- 'instatiating store entities context object
- Dim ctx As New StoreEntities
- 'retrieving product objects into a list of product objects
- Dim products As List(Of product) = (From p In ctx.products Order By p.name).ToList()
- Dim products_for_api As New List(Of product_for_api)
- For Each product In products
- Dim product_for_api As New product_for_api
- product_for_api.name = product.name
- product_for_api.price = product.price
- product_for_api.description = product.description
- products_for_api.Add(product_for_api)
- Next
- 'converting list of product objects to a list of anonymous object with properties from product object,
- 'but only what we need for service to provide
- ' Dim products_for_json = products.Select(Function(p) New With {.name = p.name, .price = p.price}).ToList()
- 'using javascript serializer to serialize list of products for json into javascript object notation format
- 'and assigning it to as string variable
- Dim json_string As String = js_serializer.Serialize(products_for_api)
- 'writing json string to response stream
- context.Response.Write(json_string)
- context.Response.ContentType = "text/plain"
- End Sub
- Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
- Get
- Return False
- End Get
- End Property
- End Class
Add Comment
Please, Sign In to add comment