多个可选查询字符串参数REST API GET [英] Multiple optional query string parameters REST API GET

查看:223
本文介绍了多个可选查询字符串参数REST API GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web API 2来实现静态服务.在对最佳做法进行了一些研究之后,每个人似乎都对如何执行以下操作有不同的意见.我有GET

I am using web api 2 to implement a restful service. After doing some research on best practices everyone seems like they are having different opinions on how to do the following. I have a GET

public HttpResponseMessage Get(string crewId, string shiftDate, int offset = 1, int limit = 10)

此GET方法返回一个列表.有多种方法可以从此方法获取数据.

This GET method returns a list. There are multiple ways of getting the data from this method.

  • 仅通过crewId获取
  • 仅通过shiftDate获取
  • 或者
  • 通过crewId和shiftDate获取
  • 您(1)是否将crewId和shiftDate标记为可选?

    Do you (1) Mark the crewId and shiftDate as optional?

    public HttpResponseMessage Get(string crewId = null, string shiftDate = null, int offset = 1, int limit = 10)
    

    然后使用一堆if语句来检查填充内容和未填充内容,以便能够执行操作

    and then have bunch of if statements to check what is filled and what is not filled to be able to do actions

    if(crewId != null && shiftDate == null){
      // Get by crewId
    }else if(crewId == null && shiftDate != null){
      // Get By shiftDate
    }else if(crewId != null && shiftDate != null){
      // Get By crewId and shiftDate
    }
    

    对于我来说,这样做似乎很疯狂,特别是如果您有很多参数,那么代码中就会有太多的"if"语句.

    To me this looks crazy to do especially if you have many parameters you would have too many "if" statements in your code.

    您(2)有不同的获取方式吗?

    Do you (2) Have different set of gets?

    public HttpResponseMessage GetByCrewId(string crewId, int offset = 1, int limit = 10)
    public HttpResponseMessage GetByShiftDate(string shiftDate, int offset = 1, int limit = 10)
    public HttpResponseMessage GetByCrewIdShiftDate(string crewId, string shiftDate, int offset = 1, int limit = 10)
    

    然后将URI路由映射到方法

    and then you would have your URI Route map to the method

  • .../api/GetByCrewId?crewId = 1234
  • .../api/GetByShiftDate?shiftDate = 1111-11-11
  • .../api/GetByCrewIdShiftDate?crewId = 1234& shiftDate = 1111-11-11
  • 选项2安静吗?

    或者有更好的选择(3).

    Or is there an better options (3).

    上述两个选项仅在确保我正在使用最佳实践并遵循REST标准的情况下起作用.似乎我正在丢失某些东西,希望您能把我带向正确的方向.

    Both options above will work just making sure I am using best practices and following REST standards. Just seems like I am missing something and hopefully you can put me in the right direction.

    推荐答案

    您不希望选项(2)成为RESTful,而您想在URL中使用名词.

    You don't want option (2) - to be RESTful you want nouns in your URL.

    所以您希望您的URL看起来像这样:

    So you want your URL's to look like:

    /api/items/?crewId=1234&shiftDate=1111-11-11
    

    (根据乘员"和班次"参数的名称,我无法确定您的物品是什么.有乘员和班次的日期是什么?钓鱼之旅?如果这样,URL会更好作为/api/fishing-trips/?crewId = ....& shiftDate = ...

    (I can't work out what your Items are, based on the 'Crew' and 'Shift' parameter names. What has a Crew and a Shift date?? Fishing trips?? If so, the url would be better as /api/fishing-trips/?crewId=....&shiftDate=...

    对于控制器,我会选择类似的东西:

    As for the controller, I'd go for something like:

    public HttpResponseMessage Get(string crewId = null, string shiftDate = null, int offset = 1, int limit = 10) {
    
        return dataSource.Where(x=> (x.CrewId == crewId || crewId == null)
                && (x.ShiftDate == shiftDate || shiftDate == null));
    }
    

    这篇关于多个可选查询字符串参数REST API GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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