Web API/REST:请求项目列表 [英] Web API / REST: Request list of items
问题描述
我想这可能是一个简单的问题,但是我看到那里的答案相互矛盾.
I guess this is probably a simple question, but I am seeing conflicting answers out there.
我正在构建一个宁静的Web api服务,该服务返回类对象.除了可以请求所有项目,一个项目或类别,例如:
I am building a restful web api service that returns class objects. In addition to being able to request ALL items, one item, or category like so:
http://MyService/Category/Blues
我还希望能够使用户能够选择多个,例如:
I also want to be able to give the users the ability to select multiple, such as:
http://MyService/Albums/12345,77777,87654
http://MyService/Category/Blues,Pop,Metal
我不需要上面显示的语法,这只是一个例子.
I don't need the syntax shown above, that was just an example.
我已经通过创建如下代码来完成此任务:
I've already accomplished this by creating code like so:
[Route("Albums/Category/{categoryList}")]
public List<Album> GetAlbumsByCategory(string categoryList)
{
int[] cats = categoryList.Split(',');
return(AlbumManager.GetAlbums(cats));
}
这可以正常工作,但是用户需要知道输入可以是单个值,也可以是逗号分隔的值列表.然后,在我的AlbumManager中,我要遍历相册集合并将仅选定的项目添加到新列表中.
This works fine, but the user needs to know that the input can either be a single value, or a list of values by comma. Then in my AlbumManager I am iterating through the album collection and adding only the selected items to the new list.
我想知道:
(A)REST中是否已经有快捷方式以及执行此操作的首选方法?我已经看到一些有关Category = Blues& Category = Pop&.....对我来说似乎很冗长,但是如果有一个标准,我想使用它.
(A) Is there already shortcuts in REST and preferred ways of doing this? I have seen some recommendations of Category=Blues&Category=Pop& ..... Seems verbose to me, but if there is a standard out there I want to use it.
(B)当我使用自己的方法时,我会不断在各处编写拆分代码.REST中是否有捷径可以简化?我已经看到一些[FromUri]参考,但对我来说还不清楚.
(B) When I use my approach, I'm constantly writing split code all over the place. Is there a shortcut in REST for this to simplify? I have seen some [FromUri] references but it's not clear to me.
推荐答案
a).休息会以一种精确的推荐方式稍微放松一点,基本上您可以选择3种方式.哪一个都不比另一个更"RESTful",请选择最适合您的API用户的一个-这可能与最适合您自己的API用户不同,毕竟所有API用户也是用户:-).
a). Rest is a little relaxed on a precise recommended way to do this, basically your have 3 options. None of which are any more "RESTful" that the other, choose the one which best suites your API user - which may be different than what suites your self, after-all API users are users too :-).
1).实现一个自定义的ActionFilter,它将使您免于拆分代码,请参见该问题的多个答案 https://stackoverflow.com/a/19107738/1422377
1). Implement a custom ActionFilter which will save you from the split code, see the multiple answers over on this question https://stackoverflow.com/a/19107738/1422377
2).听起来像反对原则",发送一个POST请求,该请求接受来自正文的列表.是的,最纯粹的说法可能是,除非您要修改数据,否则这不应该构成帖子,而要记住用户.
2). As "against principle" as this sounds, send a POST request that accepts a List from the body. Yes a purest could argue that unless you are modifying data this should not constitute a post, but remember the user.
3).稍微修改您当前的方法,以使API用户更清楚他们期望输入什么.例如:
3). Modify your current approach slightly to make it more clear to the API user what is expected to them as input. for example:
public List<Album> GetAlbumsByCategory([FromUri] string[] categoryList)
{
//your code here
}
就您而言,我个人会选择选项3-尽管语法很冗长,但任何API用户都会很熟悉,其次是选项1.如果您有更复杂的输入模型,请选择选项2.没有一个比另一个更真正的RESTful了.
Personally, in your case I would go with option 3 - although verbose the syntax is going to be familiar to any API user, followed by option 1. If you had a more complex input model go with option 2. As I said none are really any more RESTful than the other.
这篇关于Web API/REST:请求项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!