简体   繁体   English

ASP.net REST API返回本地或天蓝色的不同数据

[英]ASP.net REST API returns different data local or on azure

So I built this API that returns data about devices. 因此,我构建了此API,该API返回有关设备的数据。 When I run this project localy, the result my requests give me are correct: 当我在本地运行该项目时,我的请求给我的结果是正确的:

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

When I publish this project to Azure and make the exact same request this is what I get: 当我将此项目发布到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\":....

It looks like a string representation of a HttpResponseMessage object. 它看起来像HttpResponseMessage对象的字符串表示形式。 As you can see under contents, thats the same output of when I did the request locally. 正如您在内容下看到的那样,这与我在本地进行请求时的输出相同。 Now is the question, what am I doing wrong here? 现在的问题是,我在这里做错了什么?

Code: 码:

[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.");
}

I can swear that this worked yesterday. 我可以发誓昨天这个工作成功了。 My other endpoints work just fine.. 我的其他端点工作正常。

EDIT This endpoint gives me correct results both locally and on 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.");
        }

I create the following sample to reproduce the issue on my side, the sample that returns an HttpResponseMessage works fine both on local and Azure website. 我创建以下示例来重现我的问题,该示例返回HttpResponseMessage可以在本地和Azure网站上正常工作。 You could create a new Web API project to convert the return value directly into an HTTP response message and publish the project to a new Azure website as I did to check if it works as expected. 您可以创建一个新的Web API项目,以将返回值直接转换为HTTP响应消息,然后像我检查该项目是否按预期那样将其发布到新的Azure网站上。

My controller action 我的控制器动作

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;
     }
}

在此处输入图片说明

If possible, you could modify your controller action to return json string directly. 如果可能,您可以修改控制器操作以直接返回json字符串。

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

    return JsonConvert.SerializeObject(list);
}

Besides, please make sure you still could get correct result from action GetAllDevicesLocation from your local project. 此外,请确保您仍然可以从本地项目的操作GetAllDevicesLocation获得正确的结果。 And you could remote debug your web app to trace the variable response and check if the method CreateResponse(JsonConvert.SerializeObject(list)) works as it run on your local project. 然后,您可以远程调试Web应用程序以跟踪变量response并检查方法CreateResponse(JsonConvert.SerializeObject(list))在本地项目上运行时是否有效。

If you could provide us a sample that could reproduce the issue, which will help us find and fix the issue. 如果您可以提供一个可以重现该问题的样本,这将有助于我们发现并解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM