何时使用pathParams或QueryParams [英] When to use pathParams or QueryParams

查看:667
本文介绍了何时使用pathParams或QueryParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于什么时候应该使用URL的路径参数而不是应该使用查询参数,是否有经验法则?

Is there a rule of thumb as to when one should use path parameters for a URL versus when you should use query parameters?

说我有一张桌子发票与字段公司(PK),InvoiceNo(PK),Invoiceline,invoiceValue,noOfLines,salesPerson

Say I've got a table Invoice with the fields company(PK),InvoiceNo(PK), Invoiceline, invoiceValue, noOfLines, salesPerson

我当前的想法是你的网址应该是

My current thinking is that your URL should be along the lines of

/Invoice/

哪个会显示所有发票

/Invoice/{company}

这将显示公司的所有发票。

Which would display all invoices for the company.

/Invoice/{company}/{InvoiceNo}

显示特定发票和

/Invoice/{company}/{InvoiceNo}?invoiceLineNo=23

仅显示第23行。

我想的方式是主键字段应该是路径的一部分您要过滤的任何其他字段都是t的一部分查询参数。

The way I'm thinking is that Primary key fields should be part of the path and any other fields that you would filter on are part of the query parameter.

这听起来像是区分两者的合理方法吗?

Does this sound like a reasonable way of distinguishing between the two?

推荐答案

我个人的经验法则是PathParam引导您要求的实体类型。

My personal rule of thumb that the PathParam leads upto the entity type that you are requesting.

/Invoices             // all invoices
/Invoices?after=2011  // a filter on all invoices

/Invoices/52          // by 52
/Invoices/52/Items    // all items on invoice 52
/Invoices/52/Items/1  // Item 1 from invoice 52

/Companies/{company}/Invoices?sort=Date
/Companies/{company}/Invoices/{invoiceNo} // assuming that the invoice only unq by company?

报价罗先生用于分组数据的路径参数,用于过滤的查询参数

这篇关于何时使用pathParams或QueryParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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