我应该如何将多个参数传递给一个ASP.Net的Web API获得? [英] How should I pass multiple parameters to an ASP.Net Web API GET?

查看:272
本文介绍了我应该如何将多个参数传递给一个ASP.Net的Web API获得?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.net MVC4的Web API(希望)实现一个RESTful API。我需要在几个参数到系统,以通过,并有它执行一些动作,然后返回对象作为结果的列表。具体来说,我传递两个日期并返回落在它们之间的记录。我还保持跟踪返回的记录,以便后续调用不要让系统重新处理。

我已经考虑了几个方法:


  1. 序列化PARAMS到一个单一的JSON字符串,并在API中除了采摘它。
    http://forums.asp.net/t/1807316.aspx/1


  2. 在传递查询字符串中PARAMS。结果
    <一href=\"http://stackoverflow.com/questions/2640560/what-is-best-way-to-pass-multiple-query-parameters-to-a-restful-api\">What是通过多个查询参数,一个宁静的API的最佳方式?


  3. 定义PARAMS的路线:
    API /控制器/日期1 /日期2


  4. 使用POST的本质让我传递一个对象使用参数。


  5. 研究ODATA因为在Web API(目前)支持它。我没有做太多与此没有,所以我不是很熟悉。


看来,适当休息实践表明,当数据被上拉,你应该使用GET。然而,GET也应该nullipotent(不产生副作用),我不知道如果我的具体的实施违反了,因为我在API系统标记的记录,因此我产生副作用。

这也促使我支持可变参数的问题。如果输入参数列表的变化,这将是乏味不得不重新定义你的路线可供选择3如果出现这种情况很多。如果这些参数在运行时定义可能会发生什么......

在任何情况下,我的具体实现,它的选择(如果有的话)似乎是最好的?


解决方案

这是什么标志备案意思?如果仅用于记录的目的,我会用GET并禁用所有缓存,因为要记录这种资源的每个查询。如果这个记录标记是有其他目的,POST是要走的路。用户应该知道,他的行动影响体系和POST方法是一个警告。

I am using the .Net MVC4 Web API to (hopefully) implement a RESTful api. I need to pass in a few parameters to the system and have it perform some action, then return a list of objects as the results. Specifically I am passing in two dates and returning records that fall between them. I'm also keeping track of the records returned so that subsequent calls do not get reprocessed in the system.

I've considered a few approaches:

  1. Serializing the params into one single JSON string and picking it apart in the API. http://forums.asp.net/t/1807316.aspx/1

  2. Pass the params in the query string.
    What is best way to pass multiple query parameters to a restful api?

  3. Defining the params in the route: api/controller/date1/date2

  4. Using a POST that inherently lets me pass an object with params.

  5. Researching ODATA since the Web API (currently) supports it. I haven't done much with this yet so I'm not very familiar with it.

It seems that proper REST practices indicate when data is pulled, you should use a GET. However, GET should also be nullipotent (produces no side-effects), and I wonder if my specific implementation violates that since I mark records in the API system, hence I am producing side-effects.

It also led me to the question of supporting variable parameters. If the input parameter list changes, it would be tedious to have to re-define your route for Choice 3 if that happens a lot. And what might happen if the parameters are defined at run-time...

In any case, for my specific implementation, which choice (if any) seems best?

解决方案

What does this record marking mean? If this is used only for logging purposes, I would use GET and disable all caching, since you want to log every query for this resources. If this record marking is has another purpose, POST is the way to go. User should know, that his actions affect system and POST method is a warning.

这篇关于我应该如何将多个参数传递给一个ASP.Net的Web API获得?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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