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

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

问题描述

我正在使用 .Net MVC4 Web API(希望)实现 RESTful api.我需要向系统传递一些参数并让它执行一些操作,然后返回一个对象列表作为结果.具体来说,我传入两个日期并返回它们之间的记录.我还会跟踪返回的记录,以便后续调用不会在系统中重新处理.

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.

我考虑了几种方法:

  1. 将参数序列化为一个 JSON 字符串并在 API 中将其分开.http://forums.asp.net/t/1807316.aspx/1

在查询字符串中传递参数.
什么是最好的传递方式一个restful api的多个查询参数?

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

定义路由中的参数:api/控制器/date1/date2

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

使用 POST 固有地让我传递带有参数的对象.

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

正在研究 ODATA,因为 Web API(当前)支持它.我还没有做太多,所以我对它不是很熟悉.

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.

似乎适当的 REST 实践表明何时拉取数据,您应该使用 GET.但是,GET也应该是无效的(不会产生副作用),我想知道我的具体实现是否违反了这一点,因为我在API系统中标记了记录,因此我产生了副作用.

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.

这也让我想到了支持可变参数的问题.如果输入参数列表发生变化,如果这种情况经常发生,则必须为选择 3 重新定义您的路线将是乏味的.如果在运行时定义参数会发生什么...

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?

推荐答案

这个记录标记是什么意思?如果这仅用于记录目的,我将使用 GET 并禁用所有缓存,因为您希望记录此资源的每个查询.如果记录标记有其他目的,POST 是最佳选择.用户应该知道,他的行为会影响系统,POST方法是一个警告.

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 record marking has another purpose, POST is the way to go. User should know, that his actions effect the system and POST method is a warning.

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

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