因此,我構建了此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.");
}
這是一個Web應用程序或不同的東西? – Kamo
這是一個WebApp.- – knuddan
您將不得不提取內容標籤,然後對該特定標籤使用反序列化。或者您將需要修改反序列化以包含響應處理 –