【问题标题】:Is it possible to not return anything from a VB/ASP.net 2.0 WebMethod, so that I can return my own data?是否可以不从 VB/ASP.net 2.0 WebMethod 返回任何内容,以便我可以返回自己的数据?
【发布时间】:2014-12-03 21:11:04
【问题描述】:

基本上,我正在打一场艰苦的战斗,以一种调用方想要的方式格式化我返回的数据。

我发现我可以“完美”地“响应。写入”数据,但 WebMethod 坚持要返回一些东西……即使它是空的。

<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

有什么方法可以抑制这个 null 吗?

【问题讨论】:

  • 尝试在刷新后添加一个 HttpContext.Current.Response.CompleteRequest()。
  • @Gridly - 我无法让 CompleteRequest 没有编译错误。我尝试了您所拥有的以及 HttpApplication.CompleteRequest()... 但是... HttpContext.Current.Response.End() 似乎已经解决了我的问题!!!
  • 我的错。它应该是 HttpContext.Current.ApplicationInstance.CompleteRequest()。有关 Response.End() 和 ApplicationInstance.CompleteRequest() 之间区别的信息,请参阅 thisthis。简短的回答是使用 ApplicationInstance.CompleteRequest()
  • @Gridly - 不确定我的评论去了哪里,我告诉过你“谢谢,我已经修改了我的代码。”但是...... .CompleteRequest 在末尾添加了“null”......所以我必须使用.End。 (如果没有你的帮助,我仍然不会发现,再次感谢!!!)

标签: asp.net vb.net


【解决方案1】:

将其设为 Sub 而不是函数:

<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

【讨论】:

  • 不幸的是,它仍然返回一个空值。 {stuffhere}空
  • 如果 {} 为空的结果集为空,你能返回吗?
  • 结果集存在,我只想自己写而不是让 WebMethod 来做,但似乎 Web Method 必须返回一些东西。 (由于这个问题:stackoverflow.com/questions/27220728/…
猜你喜欢
  • 2012-07-11
  • 2020-01-29
  • 2020-10-03
  • 2010-12-22
  • 1970-01-01
  • 2019-04-02
  • 2019-02-25
  • 2022-01-23
  • 2011-07-30
相关资源
最近更新 更多