1

Basically I am fighting an uphill battle to format the data I am returning in a way that the calling party wants it.

I have found that I can "response.write" the data "perfectly" but the WebMethod insists on returning something... even if it's a null.

<System.Web.Services.WebMethod> _
Public Shared Function api_method(ByVal key1 As String) As String

    Dim test As Object = getReturnData(key1)

    Dim json As String = New JavaScriptSerializer().Serialize(test)

    json = Replace(json, """amount"":""", """amount"":")
    json = Replace(json, """,""currency", ",""currency")

    HttpContext.Current.Response.BufferOutput = True
    HttpContext.Current.Response.ContentType = "application/json"
    HttpContext.Current.Response.Write(json)
    HttpContext.Current.Response.Flush()

End Function

Any way to suppress that null?

msimmons
  • 146
  • 1
  • 3
  • 15
  • 1
    Try adding a HttpContext.Current.Response.CompleteRequest() after the flush. – Gridly Dec 04 '14 at 02:12
  • @Gridly - I couldn't get CompleteRequest to no have a compile error. I tried what you have as well as HttpApplication.CompleteRequest()... HOWEVER... HttpContext.Current.Response.End() seems to have resolved my issue!!! – msimmons Dec 04 '14 at 12:47
  • 1
    My bad. it should have been HttpContext.Current.ApplicationInstance.CompleteRequest(). See [this](http://stackoverflow.com/questions/10603553/response-end-vs-httpcontext-current-applicationinstance-completerequest) and [this](http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful) for information about the difference between Response.End() and ApplicationInstance.CompleteRequest(). The short answer is to use ApplicationInstance.CompleteRequest() – Gridly Dec 05 '14 at 02:14
  • @Gridly - Not sure where my comment went where I told you "Thanks, I've modified my code." HOWEVER... .CompleteRequest adds "null" at the end... So I have to use .End. (I still wouldn't have found that without your help, so thanks again!!!) – msimmons Dec 08 '14 at 15:15

1 Answers1

1

Make it a Sub instead of a function:

<System.Web.Services.WebMethod> _
Public Shared Sub api_method(ByVal key1 As String)

    Dim test As Object = getReturnData(key1)

    Dim json As String = New JavaScriptSerializer().Serialize(test)

    json = Replace(json, """amount"":""", """amount"":")
    json = Replace(json, """,""currency", ",""currency")

    HttpContext.Current.Response.BufferOutput = True
    HttpContext.Current.Response.ContentType = "application/json"
    HttpContext.Current.Response.Write(json)
    HttpContext.Current.Response.Flush()

End Sub
Joce Pedno
  • 334
  • 1
  • 7
  • Unfortunately that still returns a null. {stuffhere}null – msimmons Dec 03 '14 at 21:21
  • Can you just return {} if it is null for an empty result set? – RickJames Dec 03 '14 at 21:40
  • The result set is present, I just want to write it myself rather than having the WebMethod do it, but it seems the Web Method HAS to return something. (due to this issue: http://stackoverflow.com/questions/27220728/returning-a-decimal-value-in-json-via-a-vb-asp-net-2-0-webmethod-how-can-i-make ) – msimmons Dec 03 '14 at 21:48