从Web API返回JsonResult,不包含其属性 [英] Return JsonResult from web api without its properties
问题描述
我有一个Web API控制器,从那里我从一个操作返回一个对象作为JSON.
I have a Web API controller and from there I'm returning an object as JSON from an action.
我正在这样做:
public ActionResult GetAllNotificationSettings()
{
var result = new List<ListItems>();
// Filling the list with data here...
// Then I return the list
return new JsonResult { Data = result };
}
但是通过这种方式,包含其Data
属性的JsonResult对象被序列化为JSON.因此,该操作返回的最终的JSON如下所示:
But this way the JsonResult object including its Data
attribute is serialized as JSON. So my final JSON that is return by the action looks like this:
{
"ContentEncoding": null,
"ContentType": null,
"Data": {
"ListItems": [
{
"ListId": 2,
"Name": "John Doe"
},
{
"ListId": 3,
"Name": "Jane Doe"
},
]
},
"JsonRequestBehavior": 1,
"MaxJsonLength": null,
"RecursionLimit": null
}
我无法序列化此JSON字符串,因为JsonResult
对象向其添加了各种其他属性.我只对ListItems
感兴趣,仅此而已.但是它会自动添加诸如:ContentType
,MaxJsonLength
等...
I can't serialize this JSON string because the JsonResult
object added all kinds of other properties to it. I'm only interested in ListItems
, nothing else. But it automatically added things like: ContentType
, MaxJsonLength
etc...
由于JSON字符串中的所有其他属性,现在这对我不起作用...
Now this won't work for me because of all the other properties in the JSON string...
var myList = JsonConvert.DeserializeObject<List<ListItems>>(jsonString);
是否有一种方法可以从操作中发送JSON对象,从而不会添加我不需要的所有属性?
Is there a way to send a JSON object from the action so that it won't add all the properties that I dont need?
推荐答案
作为使用ASP.NET API已有3年的人,我建议您返回HttpResponseMessage.不要使用ActionResult或IEnumerable!
As someone who has worked with ASP.NET API for about 3 years, I'd recommend returning an HttpResponseMessage instead. Don't use the ActionResult or IEnumerable!
ActionResult不好,因为您已经发现.
ActionResult is bad because as you've discovered.
返回IEnumerable<>不好,因为您可能想要稍后对其进行扩展并添加一些标头等.
Return IEnumerable<> is bad because you may want to extend it later and add some headers, etc.
使用JsonResult是不好的,因为您应该允许您的服务可扩展并支持其他响应格式,以防将来万一.如果您真的想限制它,可以使用操作属性(而不是在操作主体中)进行限制.
Using JsonResult is bad because you should allow your service to be extendable and support other response formats as well just in case in the future; if you seriously want to limit it you can do so using Action Attributes, not in the action body.
public HttpResponseMessage GetAllNotificationSettings()
{
var result = new List<ListItems>();
// Filling the list with data here...
// Then I return the list
return Request.CreateResponse(HttpStatusCode.OK, result);
}
在测试中,我通常使用以下帮助器方法从HttpResponseMessage中提取对象:
In my tests, I usually use the below helper method to extract my objects from the HttpResponseMessage:
public class ResponseResultExtractor
{
public T Extract<T>(HttpResponseMessage response)
{
return response.Content.ReadAsAsync<T>().Result;
}
}
var actual = ResponseResultExtractor.Extract<List<ListItems>>(response);
通过这种方式,您达到了以下目标:
In this way, you've achieved the below:
- 您的操作还可以返回错误消息和未找到的状态代码(例如404),因此通过上述方式,您可以轻松地处理它.
- 您的操作不仅限于JSON,还支持JSON,具体取决于客户端的请求首选项和Formatter中的设置.
查看以下内容: http://www.asp.net /web-api/overview/formats-and-model-binding/内容协商
这篇关于从Web API返回JsonResult,不包含其属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!