PDA

View Full Version : [CLOSED] Load tree with WebAPI call



jchau
Apr 21, 2015, 9:19 PM
I set a tree's loader to a webapi action. What should be the return datatype for that action? If I return string, the tree doesn't load. I also tried returning object, but in both cases, response is wrapped in quotes.



<HttpGet> _
Public Function GetNodes() As Object
Dim nodes = GetTreeNodes()
Return nodes.ToJson()
End Function

Daniil
Apr 22, 2015, 4:42 PM
Hi @jchau,

This example might bring some clues:
http://examples.ext.net/#/TreePanel/Loaders/Using_Handler

The key points that TreeLoader.ashx sets

context.Response.ContentType = "text/json";
and writes

context.Response.Write(nodes.ToJson());


response is wrapped in quotes

So, it might be OK if you set `ContentType` to `"text/json"`. Though, not sure there is such a possibility with WebAPI.

jchau
Apr 22, 2015, 4:53 PM
Thanks. I will investigate further. The response content type is automatically application/json for web APIs but maybe it has to be text/json.

jchau
Apr 22, 2015, 6:31 PM
This works:



Return New HttpResponseMessage() With {.Content = New StringContent(changes.ToJson, Encoding.UTF8, "text/json")}


So I just wrapped that up in a class for easy reuse.


Public Class RawJSONResponseMessage
Inherits HttpResponseMessage


Public Sub New(json As String)
Me.Content = New StringContent(json, Encoding.UTF8, "application/json")
End Sub


End Class

Daniil
Apr 23, 2015, 7:45 AM
Nice. Thank you for sharing the solution!