函数的意外返回值

时间:2014-06-18 19:41:13

标签: asp.net vb.net visual-studio-2012 asp.net-web-api

长期潜伏者,第一次海报。

我正在编写WEBAPI并对默认输出有一些疑问。我知道输出是基于请求中的ACCEPT(json / xml等)。但是,我需要完全控制输出。我将格式化自己的XML。

我找到了几个关于如何执行此操作的示例,大部分都是在.cs中,因此我尝试将其更改为vb.net。

这是我的一个测试:

      Public Function Authenticate() As HttpResponseMessage
      Dim XML As String = "Message content"
      Return New HttpResponseMessage() With { _
      .Content = New StringContent(XML, Encoding.UTF8, "application/xml") _
      }
    End Function

在我的通话功能中,当我尝试时: 返回Authenticate()

我收到错误: 类型的价值' System.net.http.httpresponsemessage'无法转换为String

似乎在.cs中使用了很多,因为我看到很多例子。任何人都知道如何在VB.NET中实现这一目标?

我的控制器:

<HttpGet, HttpPost> _
Public Function [Login](<FromUri> staff As login) As String
      Dim xmlheader As String

      Dim head As New header
      xmlheader = head.addheader()

      If staff.password = "" Then
        Dim result As String
        Dim err As New messages
        result = xmlheader & err.geterror("password")
        Return Authenticate()
       End If
End Function

谢谢!

2 个答案:

答案 0 :(得分:0)

看起来您正在尝试将Authenticate的返回值用作字符串。我不确切知道你要做什么,但我认为.ToString()会有用。

Dim foo As String = Authenticate().ToString()
' Do whatever with foo here...use it as a string

答案 1 :(得分:0)

你的问题很清楚。你有

Public Function [Login](<FromUri> staff As login) As String

您需要将控制器声明为

Public Function [Login](<FromUri> staff As login) As HttpResponseMessage
相关问题