【问题标题】:Data models not showing up in HttpResponseMessage数据模型未显示在 HttpResponseMessage 中
【发布时间】:2017-12-26 19:38:25
【问题描述】:

我正在尝试从 .NET Core MVC 应用程序进行简单的 API 调用:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:49897");

    var response = client.GetAsync("some-route").Result;
    var dataString = response.Content.ReadAsStringAsync().Result; // Unexpected data here. See below.

    [...] // deserialize dataString
}

client.GetAsync(route) 成功命中一个 API 操作方法,它最终会这样做:

public HttpResponseMessage Get([FromUri] BindingModel bindingModel)
{
    List<SomeModel> resultObjects;

    [...] // populate resultObjects with data

    return Request.CreateResponse(HttpStatusCode.OK, resultObjects, new JsonMediaTypeFormatter());
}

dataString 最终等于:

"{\"version\":{\"major\":1,\"minor\":1,\"build\":-1,\"revision\":-1,\"majorRevision\":-1,\"minorRevision\":-1},\"content\":{\"objectType\":\"System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e\",\"formatter\":{\"indent\":false,\"serializerSettings\":{\"referenceLoopHandling\":0,\"missingMemberHandling\":0,\"objectCreationHandling\":0,\"nullValueHandling\":0,\"defaultValueHandling\":0,\"converters\":[],\"preserveReferencesHandling\":0,\"typeNameHandling\":0,\"metadataPropertyHandling\":0,\"typeNameAssemblyFormat\":0,\"typeNameAssemblyFormatHandling\":0,\"constructorHandling\":0,\"contractResolver\":null,\"equalityComparer\":null,\"referenceResolver\":null,\"referenceResolverProvider\":null,\"traceWriter\":null,\"binder\":null,\"serializationBinder\":null,\"error\":null,\"context\":{},\"dateFormatString\":\"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK\",\"maxDepth\":null,\"formatting\":0,\"dateFormatHandling\":0,\"dateTimeZoneHandling\":3,\"dateParseHandling\":1,\"floatFormatHandling\":0,\"floatParseHandling\":0,\"stringEscapeHandling\":0,\"culture\":{}}}}}"

或者,JSON 格式:

{
    version: {
        major: 1,
        minor: 1,
        [...]
    },
    content: {
        objectType: "System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"
        formatter: {
            indent: false,
            serializerSettings: {
                [...]
            }
        }
    }
}

我的模型列表根本不在其中。

究竟返回了什么,为什么响应中没有我的模型列表?我查看了几个在线资源,我似乎正在按照他们展示的方式做事。这是一个非常重要的 API 调用,所以我不确定发生了什么。

【问题讨论】:

    标签: c# asp.net .net asp.net-core asp.net-core-webapi


    【解决方案1】:

    究竟返回了什么,为什么响应中没有我的模型列表?

    返回的是您的 HttpResponseMessage 的 JSON 序列化版本,因为虽然 Web API 2 专门处理这种类型,但 ASP.NET Core Web API 没有

    准确地说,ASP.NET Core web API supports returning the following types:

    • IActionResult
    • ActionResult&lt;T&gt;

    前两个被特殊处理,但第三个导致该类型被 JSON 序列化并返回,正如您所看到的那样。

    相比之下,Web API 2 supports the following special return types

    • void
    • HttpResponseMessage
    • IHttpActionResult

    正确的解决方案是更新您的代码以使用自定义IActionResult 实现,而不是返回原始HttpResponseMessages。 You may find this guide aids you in doing so - 特别是,Microsoft.AspNetCore.Mvc.WebApiCompatShim NuGet 包有一个 ResponseMessageResult class,允许您从这里转换您的 Web API 控制器方法:

    public HttpResponseMessage Foo(Bar quux)
    {
        return new BazHttpResponseMessage();
    }
    

    到这里:

    public IActionResult Foo(Bar quux)
    {
        return new ResponseMessageResult(new BazHttpResponseMessage());
    }
    

    这应该允许您当前的代码按原样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 2019-09-22
      相关资源
      最近更新 更多