我正在尝试从 .NET Core MVC 应用程序进行简单的 API 调用:
I'm trying to make a simple API call from a .NET Core MVC application:
using (var client = new HttpClient()) { client.BaseAddress = new Uri("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 操作方法,最终执行此操作:
client.GetAsync(route) successfully hits an API action method, which ultimately does this:
public HttpResponseMessage Get([FromUri] BindingModel bindingModel) { List<SomeModel> resultObjects; [...] // populate resultObjects with data return Request.CreateResponse(HttpStatusCode.OK, resultObjects, new JsonMediaTypeFormatter()); }但是 dataString 最终等于这个:
But dataString ends up equaling this:
"{"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 格式:
Or, in JSON format:
{ 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: { [...] } } } }我的模型列表根本不在那里.
My list of models isn't in there at all.
返回的究竟是什么,为什么响应中没有我的模型列表?我查看了几个在线资源,我似乎按照他们所展示的方式做事.这是一个非常基本的 API 调用,所以我不确定发生了什么.
What exactly is being returned, and why isn't my list of models in the response? I've looked at several online resources, and I seem to be doing things the same way as they show. This is a pretty bread-and-butter API call, so I'm not sure what's going on.
推荐答案返回的究竟是什么,为什么响应中没有我的模型列表?
What exactly is being returned, and why isn't my list of models in the response?
返回的是 HttpResponseMessage 的 JSON 序列化版本,因为虽然 Web API 2 专门处理这种类型,但 ASP.NET Core Web API 不会.
What's being returned is a JSON-serialized version of your HttpResponseMessage, because while Web API 2 handles this type specially, ASP.NET Core web API does not.
准确地说,ASP.NET核心 Web API 支持返回以下类型:
- IActionResult
- ActionResult
前两个被特殊处理,但第三个导致该类型被 JSON 序列化并返回,正如您所见.
The first two are treated specially, but the third results in that type being JSON-serialized and returned, exactly as you saw.
相比之下,Web API 2 支持以下特殊返回类型:
- void
- HttpResponseMessage
- IHttpActionResult
正确的解决方案是更新您的代码以使用自定义 IActionResult 实现,而不是返回原始 HttpResponseMessage s.您可能会发现本指南可以帮助您这样做 - 在特别是,Microsoft.AspNetCore.Mvc.WebApiCompatShim NuGet 包具有ResponseMessageResult 类 允许您转换您的Web API 控制器方法来自:
The correct solution is to update your code to use a custom IActionResult implementation instead of returning raw HttpResponseMessages. You may find this guide aids you in doing so - in particular, the Microsoft.AspNetCore.Mvc.WebApiCompatShim NuGet package has a ResponseMessageResult class that allows you to convert your Web API controller method from this:
public HttpResponseMessage Foo(Bar quux) { return new BazHttpResponseMessage(); }为此:
public IActionResult Foo(Bar quux) { return new ResponseMessageResult(new BazHttpResponseMessage()); }这应该允许您当前的代码按原样工作.
which should allow your current code to work as-is.
更多推荐
数据模型未显示在 HttpResponseMessage 中
发布评论