2017-04-27 48 views
0

因此,我構建了此API以返回有關設備的數據。當我localy運行這個項目,結果我要求給我是正確的:ASP.net REST API返回本地或藍色的不同數據

[ 
    { 
    "Id": "1", 
    "Name": "x1", 
    "LocationName": "Floor 2" 
    }, 
    { 
    "Id": "2", 
    "Name": "x2", 
    "LocationName": "Floor 2" 
    } 
] 

當我發佈這個項目Azure和做出完全相同的請求,這是我得到:

"Request": { 
    "AlwaysMultipartFormData": false, 
    "JsonSerializer": { 
     "DateFormat": null, 
     "RootElement": null, 
     "Namespace": null, 
     "ContentType": "application/json" 
    }, 
    "XmlSerializer": { 
     "RootElement": null, 
     "Namespace": null, 
     "DateFormat": null, 
     "ContentType": "text/xml" 
    }, 
    "ResponseWriter": null, 
    "UseDefaultCredentials": false, 
    "Parameters": [ 
     { 
     "Name": "Accept", 
     "Value": "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml", 
     "Type": 3, 
     "ContentType": null 
     } 
    ], 
    "Files": [], 
    "Method": 0, 
    "Resource": "devices", 
    "RequestFormat": 1, 
    "RootElement": null, 
    "OnBeforeDeserialization": { 
     "Delegate": { 
     "type": "System.Action`1[[RestSharp.IRestResponse, RestSharp, Version=105.2.3.0, Culture=neutral, PublicKeyToken=null]]", 
     "assembly": "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", 
     "target": null, 
     "targetTypeAssembly": "RestSharp, Version=105.2.3.0, Culture=neutral, PublicKeyToken=null", 
     "targetTypeName": "RestSharp.RestRequest", 
     "methodName": "<.ctor>b__0", 
     "delegateEntry": null 
     }, 
     "method0": { 
     "Name": "<.ctor>b__0", 
     "AssemblyName": "RestSharp, Version=105.2.3.0, Culture=neutral, PublicKeyToken=null", 
     "ClassName": "RestSharp.RestRequest", 
     "Signature": "Void <.ctor>b__0(RestSharp.IRestResponse)", 
     "Signature2": "System.Void <.ctor>b__0(RestSharp.IRestResponse)", 
     "MemberType": 8, 
     "GenericArguments": null 
     } 
    }, 
    "DateFormat": null, 
    "XmlNamespace": null, 
    "Credentials": null, 
    "UserState": null, 
    "Timeout": 0, 
    "ReadWriteTimeout": 0, 
    "Attempts": 1 
    }, 
    "ContentType": "application/json; charset=utf-8", 
    "ContentLength": -1, 
    "ContentEncoding": "", 
    "Content": "[\r\n {\r\n \"$id\": \"1\",\r\n \"id\": \"1\",\r\n \"name\": \"x1\",\r\n \"deviceTypeName\": \"Brickstream\",\r\n \"lastSeen\":.... 

它看起來像一個HttpResponseMessage對象的字符串表示。正如你可以在內容中看到的那樣,這就是我在本地執行請求時的相同輸出。現在是問題,我在這裏做錯了什麼?

代碼:

[Route("")] 
public HttpResponseMessage GetAllDevicesLocation(string location) 
{ 
    var request = new RestRequest(); 
    request = new RestRequest($"devices", Method.GET); 
    IRestResponse response = client.Execute(request); 

    string serializedItemsFromBackend = response.Content; 
    var deserializedItemsFromBackend = JsonConvert.DeserializeObject<List<DeviceModels>>(serializedItemsFromBackend); 
    List<DeviceModels> list = new List<DeviceModels>(); 

    foreach (DeviceModels device in deserializedItemsFromBackend) 
    { 
     if (device.LocationName.Equals(location)) 
     { 
      list.Add(device); 
     } 
    } 
    if (list.Count > 0) 
    { 
     return CreateResponse(JsonConvert.SerializeObject(list)); 
    } 
    return Request.CreateResponse(HttpStatusCode.NotFound, "Devices in that area wasn't found."); 
} 

我可以發誓,這個昨天的工作。我的另一個端點工作得很好..

編輯此端點提供了本地和在Azure ::

[Route("{name}")] 
     public HttpResponseMessage GetDevice(string name) 
     { 
      var request = new RestRequest(); 
      request = new RestRequest($"devices", Method.GET); 
      IRestResponse response = client.Execute(request); 

      string serializedItemsFromBackend = response.Content; 
      var deserializedItemsFromBackend = JsonConvert.DeserializeObject<List<DeviceModels>>(serializedItemsFromBackend); 

      foreach (DeviceModels device in deserializedItemsFromBackend) 
      { 
       if (device.Name.Equals(name)) 
       { 
        return CreateResponse(JsonConvert.SerializeObject(device)); 
       } 
      } 
      return Request.CreateResponse(HttpStatusCode.NotFound, "Device with that name wasn't found."); 
     } 
+0

這是一個Web應用程序或不同的東西? – Kamo

+0

這是一個WebApp.- – knuddan

+0

您將不得不提取內容標籤,然後對該特定標籤使用反序列化。或者您將需要修改反序列化以包含響應處理 –

回答

0

我創建了下面的示例重現我這邊的問題,樣本而返回我正確的結果HttpResponseMessage在本地和Azure網站上均可正常工作。您可以創建一個新的Web API項目,將返回值直接轉換爲HTTP響應消息,並將項目發佈到新的Azure網站,就像我一樣,檢查它是否按預期工作。

我的控制器動作

public class ValuesController : ApiController 
{ 
    public HttpResponseMessage Get() 
    { 
     List<DeviceModels> list = new List<DeviceModels>(); 
     list.Add(new DeviceModels() { Id = "1", Name = "x1", LocationName = "Floor 2" }); 
     list.Add(new DeviceModels() { Id = "2", Name = "x2", LocationName = "Floor 2" }); 

     HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(list)); 

     return response; 
    } 
} 

enter image description here

如果可能的話,你可以修改你的控制器動作直接返回JSON字符串。

public string GetAllDevicesLocation(string location) 
{ 
    //code logic 

    return JsonConvert.SerializeObject(list); 
} 

此外,請確保你仍然可以從行動GetAllDevicesLocation從您的本地項目得到正確的結果。你可以remote debug your web app跟蹤變量response並檢查方法CreateResponse(JsonConvert.SerializeObject(list))是否在你的本地項目上運行。

如果您可以向我們提供一個可能重現此問題的示例,這將幫助我們找到並解決問題。