如何做到使用ASP.Net网页API 2部分缓解 [英] How to do partial responses using ASP.Net Web Api 2

查看:125
本文介绍了如何做到使用ASP.Net网页API 2部分缓解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很新的API设计和MVC的概念,但据我所知,像GET / API /产品应该返回一个产品列表和GET / API /产品/ 1应返回的单品。在速度方面,我的感觉是/ API /产品应该返回的信息较少,即只是标识和名称,而/ API /产品/ 1应该回到更即ID,名称和说明。

I'm really new to API design and MVC concepts, but as far as I can tell, something like GET /api/products should return a list of products and GET /api/products/1 should return a single product. In terms of speed my feeling is that /api/products should return less information i.e. just id and name, whereas /api/products/1 should return more i.e. id, name, and description.

据我所看到的,来处理这个最好的办法就是让在/ API /产品端点不退换产品类的某些字段。这是在/ API /产品?域=名称的情况下尤为必要。我使用ASP.Net网页API 2和曾尝试以下内容:

As far as I can see, the best way to handle this is to make certain fields of the product class not be returned in the /api/products endpoint. This is especially necessary in the case of /api/products?fields=name . I'm using ASP.Net Web Api 2 and have tried the following:


  • http://www.nuget.org/packages/WebApi.PartialResponse/ - 在安装此软件包引起了集版本错误

  • 在产品领域添加ShouldSerialize方法。至于原因,我不会进入这里,这种方法有点麻烦了。

  • 在<一个看着href=\"http://stackoverflow.com/questions/13606675/asp-net-web-api-partial-response-json-serialization\">ASP.NET网页API部分缓解JSON序列化,但似乎有没有成为定论那里。

  • ASP.NET的WebAPI和部分反应建议使用某产品类可空所有领域。我不知道我的理解到底是什么在那里做。

  • http://www.nuget.org/packages/WebApi.PartialResponse/ - installing this package caused an assembly version error.
  • Adding ShouldSerialize methods on the Product fields. For reasons I won't go into here, this method is a little cumbersome.
  • Looked at ASP.NET Web API partial response Json serialization but there doesn't seem to be a conclusive answer there.
  • ASP.NET WebApi and Partial Responses suggests using a product class with all nullable fields. I'm not sure I understand exactly what to do there.

有没有简单的方法做我想要做什么?

Is there any simple way to do what I'm trying to do?

否则你能提出一个更好的API设计比我在做什么?

Otherwise could you suggest a better API design than what I'm doing?

推荐答案

您也可以使用WebApi.PartialResponse(<一个href=\"http://www.nuget.org/packages/WebApi.PartialResponse/\">http://www.nuget.org/packages/WebApi.PartialResponse/).这是一个包我写了使用LINQ to JSON(Json.NET)操纵返回的对象它使用的字段语法在他们的API,例如使用由谷歌:

You could also use WebApi.PartialResponse (http://www.nuget.org/packages/WebApi.PartialResponse/). It's a package I wrote which uses LINQ to JSON (Json.NET) to manipulate the returned objects. It uses the fields syntax used by Google in their API's, eg.:


  • 栏=项目/ ID,playlistItems /片段/标题,playlistItems /片段/位置

  • 栏=项目(ID,片段/标题,片段/位置)

  • 栏=项目(ID,片段(职称,职务))

您可以找到GitHub的项目页面上的信息。 https://github.com/dotarj/PartialResponse

You can find more information on the GitHub project page: https://github.com/dotarj/PartialResponse.

这篇关于如何做到使用ASP.Net网页API 2部分缓解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆