How to specify a query parameter in a custom HTTP route of an Azure Function?(如何在 Azure Function 的自定义 HTTP 路由中指定查询参数?)
问题描述
我有一个 Azure 函数,我想设置一个自定义 HTTP 端点.在回答这个 SO 问题,我最终得到了这样的结果:
I have an Azure Function and I want to set a custom HTTP endpoint. Following the answer to this SO question, I ended up with something like this:
[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)
{
//
}
但是,该路由不被 Webjob 接受:
However, the route is not accepted by the Webjob:
"v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}"
原因是因为问号'?':
The reason is because of the question mark '?':
创建名为DoSomething"的路由时出错,并且模板'api/v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}'.文字部分 'products?manufacturer=' 无效.文字部分不能包含?"特点.参数名称:routeTemplate文字部分 'products?manufacturer=' 无效.文字部分不能包含?"字符.
An error occurred while creating the route with name 'DoSomething' and template 'api/v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}'. The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character. Parameter name: routeTemplate The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character.
问题
如何在 Azure Function 的自定义 HTTP 端点中指定查询参数?
How can I specify a query parameter in a custom HTTP endpoint of my Azure Function?
推荐答案
恐怕不能把查询参数放到Route里.
I am afraid it's not possible to put query parameter in Route.
Microsoft.AspNetCore.Routing:文字部分 'products?manufacturer=' 无效.文字部分不能包含?"字符.
Microsoft.AspNetCore.Routing: The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character.
它是 ASP.NET Routing 的内置限制,Azure Function 使用它来构建 Http 触发器的路由.
It's a built-in restriction of ASP.NET Routing, which is used by Azure Function to build route for Http trigger.
允许我将值作为 Run 的方法参数之一获取,而不是戳到 HttpRequest 实例
allow me to get the value as one of the Run's method parameters instead of poking at the HttpRequest instance
如果是你想在路由中放入查询参数的原因,我建议你添加 IDictionary<string, string>在方法签名中查询
,并使用query["manufacturer"]
来访问函数代码中的参数.但老实说,它与 request.Query["manufacturer"]
几乎相同.
If it's the reason why you want to put query parameter in route, I would suggest you add IDictionary<string, string> query
in method signature and use query["manufacturer"]
to access the parameter in function code. But honestly it's almost the same as request.Query["manufacturer"]
.
或者您可能必须遵循建议,将查询参数转换为 products/{productId}
之类的路由.
Or you may have to follow the recommendation, transform the query parameter to route like products/{productId}
.
这篇关于如何在 Azure Function 的自定义 HTTP 路由中指定查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Azure Function 的自定义 HTTP 路由中指定查询参数?


- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01