C# 数据模型未显示在HttpResponseMessage中
我正在尝试从.NET核心MVC应用程序进行一个简单的API调用:C# 数据模型未显示在HttpResponseMessage中,c#,asp.net,.net,asp.net-core,asp.net-core-webapi,C#,Asp.net,.net,Asp.net Core,Asp.net Core Webapi,我正在尝试从.NET核心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().R
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());
}
或者,以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调用,所以我不确定到底发生了什么
究竟返回了什么,为什么我的模型列表不在响应中
返回的是HttpResponseMessage的JSON序列化版本,因为虽然Web API 2专门处理这种类型,但ASP.NET核心Web API并不
准确地说:
IActionResult
ActionResult
- :
void
HttpResponseMessage
IHttpActionResult
- -特别是,NuGet软件包具有一个允许您从以下内容转换Web API控制器方法的:
为此:public HttpResponseMessage Foo(Bar-qux) { 返回新的BazHttpResponseMessage(); }
这将允许当前代码按原样工作public IActionResult Foo(Bar-quox) { 返回新的ResponseMessageResult(new BazHttpResponseMessage()); }
{ 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: { [...] } } } }