我在查看此帖子:Returning raw json (string) in wcf。我覺得我運行到一定程度了同樣的問題 我有一個返回JSON REST服務,請參見下面的代碼:返回原始JSON字符串,無轉義字符
IRestServiceImpl.vb
Imports System.ServiceModel
Imports System.ServiceModel.Web
Namespace RestService
' NOTE: You can use the "Rename" command on the context menu to change the interface name "IRestServiceImpl" in both code and config file together.
<ServiceContract()> _
Public Interface IRestServiceImpl
<OperationContract()> _
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Xml, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="xml/{id}")> _
Function XMLData(ByVal id As String) As String
'WebMessageBodyStyle.Bare WAS WebMessageBodyStyle.wrapped
<OperationContract()> _
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="api/objects/json/?lat={lat}&lon={lon}&radius={radius}&cat={cat}")> _
Function JSONData(ByVal lat As String, ByVal lon As String, ByVal radius As String, ByVal cat As String) As String
'radius in meters
End Interface
End Namespace
RestServiceImpl.vb
Namespace RestService
Public Class RestServiceImpl
Implements IRestServiceImpl
Public Function XMLData(ByVal id As String) As String _
Implements IRestServiceImpl.XMLData
Return "XML You requested product " & id
End Function
Public Function JSONData(ByVal lat As String, ByVal lng As String, ByVal d As String, ByVal cat As String) As String _
Implements IRestServiceImpl.JSONData
'returns the results JSON in format
'Return "JSON lat=" + lat + " lng=" + lng + " d=" + d + " cat=" + cat
Dim sBuilder As New StringBuilder
sBuilder.Append("{""hotspots"": [")
sBuilder.Append("{""id"": ""test_1"",")
sBuilder.Append("""anchor"": { ""geolocation"": { ""lat"": 52.3729, ""lon"": 4.93 } }, ")
sBuilder.Append("""text"": {")
sBuilder.Append("""title"": ""The Layar Office"", ")
sBuilder.Append("""description"": ""The Location of the Layar Office"", ")
sBuilder.Append("""footnote"": ""Powered by Layar"" },")
sBuilder.Append("""imageURL"": ""http:\/\/custom.layar.nl\/layarimage.jpeg"",")
sBuilder.Append("}")
sBuilder.Append("],")
sBuilder.Append("""layer"": ""mytest"",")
sBuilder.Append("""errorString"": ""ok"", ")
sBuilder.Append("""errorCode"": 0")
sBuilder.Append("} ")
Return sBuilder.ToString
End Function
End Class
End Namespace
基於上面的代碼我得到這個迴應:
這給了我在Chrome瀏覽器中的這種響應: {「JSONDataResult 「:」{\「hotspots \」:[{\「id \」:\「test_1 \」,\「anchor \」:{\「geolocation \」:{\「lat \」:52.3729,\「 「:4.93}},\」text \「:{\」title \「:\」The Layar Office \「,\」description \「:\」Layar Office \「的位置\」腳註\「: \「Powered by Layar \」},\「imageURL \」:\「http:\/\/custom.layar.nl \ /layarimage.jpeg \」,}],\「layer \」:\「mytest \」 ,\「errorString \」:\「ok \」,\「errorCode \」:0}「}
我知道反斜槓是在我的迴應中,因爲在其他線程中描述的問題(因爲我使用WebMessageFormat以.json)。
但是我不確定如何實現提供了上http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx,http://msdn.microsoft.com/en-us/library/ms789010.aspx和http://msdn.microsoft.com/en-us/library/cc681221(VS.90).aspx
現在我改變了我的Irestserviceimpl.vb的代碼示例:
Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.IO
Namespace RestService
' NOTE: You can use the "Rename" command on the context menu to change the interface name "IRestServiceImpl" in both code and config file together.
<ServiceContract()> _
Public Interface IRestServiceImpl
<OperationContract()> _
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Xml, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="xml/{id}")> _
Function XMLData(ByVal id As String) As String
'WebMessageBodyStyle.Bare WAS WebMessageBodyStyle.wrapped
<OperationContract()> _
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Bare, UriTemplate:="api/objects/json/?lat={lat}&lon={lon}&radius={radius}&cat={cat}")> _
Function JSONData(ByVal lat As String, ByVal lon As String, ByVal radius As String, ByVal cat As String) As String
'radius in meters
End Interface
Public Class RawService
<OperationContract(), WebGet()> _
Public Function GetValue() As System.IO.Stream
Dim result As String = "Hello world"
Dim resultBytes As Byte() = Encoding.UTF8.GetBytes(result)
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"
Return New MemoryStream(resultBytes)
End Function
End Class
End Namespace
,但我仍然不確定如何調用網址或準確地放置哪些代碼...如果有人可以幫助我在這裏開始?
謝謝!
我總是打開更好的建議:) 所以你說WCFWebAPI允許我提供客戶端(比如我的Windows Phone)調用返回JSON的REST服務? 如果是這樣,我現在正在閱讀「入門」教程,但它對我還不清楚。它說我需要創建ASP.NET MVC 3應用程序。但是我已經有了一個標準的解決方案,只有在嘗試創建一個新項目時纔會看到MVC2選項。所以: 1.我如何確保我可以創建MVC3項目? 2.如何將功能添加到我現有的(我猜非MVC項目)? 謝謝! :) – Flo
WCFWebApi不需要* MVC - 我已經與asp.net形式以及MVC甚至NancyFx一起運行它。只要你設置路由部分一切都會好的。 –
我下載了示例應用程序,但這給了我錯誤「解決方案中的一個或多個項目未正確加載」。該安裝不支持該項目類型。 所以我嘗試添加路由部分到我的全局。asax file: Public Shared Sub RegisterRoutes(routes As RouteCollection) End Sub \t 我還將示例應用程序bin文件夾中的所有dll複製到我自己的bin文件夾中。並導入所有類('在C#中'使用') 但我得到: -類型'RouteCollection'未定義 - 在導入'System.Routing'中指定的名稱空間或類型不包含任何公共成員或不能被發現。 – Flo