我想让我的RESTful API非常可预测.决定何时使用URI而不是使用查询参数来分段数据的最佳做法是什么.
对我来说,支持分页,排序和分组的系统参数在'?'之后是有道理的.但是如果像'status'和'region'这样的字段或其他属性来分割你的收藏呢?如果那些也是查询参数,那么知道何时使用路径参数的经验法则是什么?
我有一个 Azure 函数,我想设置一个自定义 HTTP 端点。在回答这个 SO question 之后,我得到了这样的结果:
[FunctionName("DoSomething")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}")]
HttpRequest request, ILogger logger, string tenantId, string locationId, string manufacturer)
{
//
}
Run Code Online (Sandbox Code Playgroud)
但是,Webjob 不接受该路由:
"v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}"
Run Code Online (Sandbox Code Playgroud)
原因是因为问号“?”:
创建名称为“DoSomething”且模板为“api/v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}”的路由时出错。文字部分 'products?manufacturer=' 无效。文字部分不能包含“?” 特点。参数名称:routeTemplate 文字部分 'products?manufacturer=' 无效。文字部分不能包含“?” 特点。
题
如何在 Azure 函数的自定义 HTTP 终结点中指定查询参数?
我尝试[FromQuery]与 Azure Function v3 一起使用,但收到以下错误:
无法将参数“search”绑定到字符串类型。
对于以下方法:
[FunctionName("GetObjects")]
public ActionResult<IActionResult> QueryObjects(
[HttpTrigger(AuthorizationLevel.Function, "GET", Route = "objects")]
HttpRequest req,
ILogger log,
[FromQuery] string search = null)
{
//do some stuff
}
Run Code Online (Sandbox Code Playgroud)
不[FromQuery]支持吗?
我应该使用req.Query["search"]来获取查询参数吗?
从functions.desp.json
与绑定相关
"Microsoft.Extensions.Configuration.Binder/3.1.1": {
"dependencies": {
"Microsoft.Extensions.Configuration": "3.1.2"
},
"runtime": {
"lib/netcoreapp3.1/Microsoft.Extensions.Configuration.Binder.dll": {
"assemblyVersion": "3.1.1.0",
"fileVersion": "3.100.119.61404"
}
}
},
Run Code Online (Sandbox Code Playgroud)