Implementing a JSON Channel on an ASP.NET 2.0 Web Application Using JSON.NET

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!

+3
source share
2 answers

You will certainly want to use a handler in this situation, since it will be much less overhead than the actual .aspx page.

Besides the lack of error handling, your implementation looks pretty good from here.

JSON.Net ( DateTime), . JSON- WCF . , , .Net 2.0

UPDATE:

, . ... .

, , (, , ).

  • API
    • - , , - , . , . .

    • , , , , . , , .
    • : ", !", (, LOB) , .
  • !
    • , ! , . , , . JSON , - .
    • pronto. , , .

JSON :

{
    data{...},
    error{...}
}

, , , - .

, . , , . , , .

+2

SubSonic 2.x, RESTHandler, , . / /SubSonic/HttpHandlers/RESTfullUrl.cs, XML/RSS/JSON.

, .

/// http://domain/service_directory/table_or_view/[key].[format]
/// or with a REST command
/// http://domain/service_directory/table_or_view/[command].[format]?[params]
/// for search, this is
/// http://domain/service_directory/table_or_view/search.[format]?paramname=paramvalue
/// SPs use
/// http://domain/service_directory/spname/exec.[format]?[params]

, , , . .

0

Source: https://habr.com/ru/post/1775702/


All Articles