I was tasked with embedding the JSON feed on the asp.net website, which will be used by third-party applications (such as IPhone, Android, etc.), and I would like to follow best practices.
An example of what I would like to achieve would be like: http://api.entertainment.ie/restaurants/listbycounty.asp
I chose the JSON.net api, as this is apparently recommended.
My Google-fu should fail, as I cannot find any complete code example for the Asp.net web application with JSON.net implemented on it, so I have no real concept of where to start.
My question is pretty simple:
- Should I create this feed type as an aspx or ashx file? Or even the .NET Web Service? (Remember that the tools that will use this channel are located on external applications such as IPhones, etc.).
I created a test channel in both ASHX and ASPX ... here is the code (I use Subsonic to populate the collection) ... are they one of them along the correct lines?
Aspx:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.ContentType = "application/json"
Dim objWCounty As New WLocation.wCounty
Dim objVwCounty As ICollection(Of VwCounty) = objWCounty.GetCountyFromCountryID(Enums.Country.Ireland, True)
Dim sJSON As String = JsonConvert.SerializeObject(objVwCounty)
Response.Write(sJSON)
End Sub
ASHX:
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "application/json"
Dim objWCounty As New WLocation.wCounty
Dim objVwCounty As ICollection(Of VwCounty) = objWCounty.GetCountyFromCountryID(Enums.Country.Ireland, True)
Dim sJSON As String = JsonConvert.SerializeObject(objVwCounty)
context.Response.Write(sJSON)
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Many thanks!
Peter source
share