I'm new to web API, and all the examples are in C# which doesn't help, as such I've hit a roadblock early on. I'm returning data from SQL, putting it an object array list, and then trying to return it so it could be read by a JSON client.

This is my default GET on my controller:

Function Index() As IEnumerable(Of String)
            Dim Sites As New ArrayList
            Dim dt As DataTable = DataLayer.ExecuteNoParamStoredProcedure("stored_proc", "connection_string")

            For Each r As DataRow In dt.Rows()
                Sites.Add(New SiteDetails(r("SiteName"),r("SiteId")))
            Next

            Return Sites
        End Function

And here is my SiteDetails class:

Public Class SiteDetails
Public site As String
Public siteid As String

Public Sub New(sitename As String, id As String)
    site = sitename
    siteid = id
End Sub
End Class

Questions: How to I make it output the data on the page in a format that isn't either an error or just the object name? Is there a better way to do what I'm doing?

Joe

EDIT just to comment, the above doesn't work from the off, because it can't the return type (IEnumerable(Of String)) doesn't allow an arraylist to be returned, this is the bulk of my issue.

share|improve this question
    
DO NOT ever use ArrayList. This is still there only for backwards compatibility to old code. Use strongly-typed List (Of T) instead – T.S. Oct 27 '16 at 16:19
up vote 1 down vote accepted

In your sample, you return an IEnumerable(Of String). Therefore, when returning the result of your action method, all objects are converted to a string by calling the ToString method (I suspect you have OPTION STRING turned OFF in your project); in your case, the default implementation of ToString is used. This implementation returns the name of the type.

In order to return the data, I'd change the code as follows:

  • If you want to return all object properties, change the signature of your action method to Function Index() As IEnumerable(Of SiteDetails)
  • If you want to return some strings, leave the signature of your action method as it is, but override the ToString method of the SiteDetails class so that the required data are formatted as a string correctly.

In addition, I'd propose not to use the ArrayList class anymore; there is a strongly typed List(Of T) class (in your case List(Of SiteDetails) if you want to return the SiteDetails objects or List(Of String) if you want to return strings).


Also, make sure that you are using a Web API controller. These controllers are derived from the ApiController class (as opposed to a MVC controller that looks roughly the same, but is derived from System.Web.Mvc.Controller).

share|improve this answer
    
In all honesty, I only loosely grasp what you mean, I've amended as follows: changed my return type to As IEnumerable(Of SiteDetails) and changed my sites array to be: Dim Sites As New List(Of SiteDetails) - the output on the page is now System.Collections.Generic.List1[OnsiteAPI.SiteDetails]` - where did I go wrong? Thanks for getting back to me btw :) – fl3rgle Oct 27 '16 at 15:17
    
On which page are you putting the output? I've created a sample project with your SiteDetails class and a List(Of SiteDetails) in a method that returns IEnumerable(Of SiteDetails). In Edge, the data is shown in JSON format: [{"site":"site1","siteid":"1"},{"site":"site2","siteid":"2"}‌​]. – Markus Oct 27 '16 at 15:52
    
Does your controller class inherit from ApiController? It seems that you are using a MVC controller instead of a Web API controller. In the sample project, you should put the API code into the ValuesController; the HomeController class is a MVC controller. I've put my sample code into HomeController and the result is System.Collections.Generic.List``1[TestWebAPI.SiteDetails] as in your comment. So it seems that you are not using a API controller. – Markus Oct 27 '16 at 15:52
    
I feel like a fool! This fixed it, it was in fact an MVC controller, not a web API controller, thanks! :D – fl3rgle Oct 27 '16 at 16:42
    
You're welcome-the differences are really small so it can be easily overlooked in the heat of the action. – Markus Oct 27 '16 at 16:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.