c# - 如何在Azure函数的自定义HTTP路由中指定查询参数?

标签 c# azure azure-functions

我有一个 Azure Function,我想设置自定义 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)
{
        // 
}

但是,Webjob 不接受该路由:

"v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={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 端点中指定查询参数?

最佳答案

恐怕无法将查询参数放入路由中。

Microsoft.AspNetCore.Routing: The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character.

这是 ASP.NET 路由的内置限制,Azure Function 使用它来为 Http 触发器构建路由。

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在方法签名中并使用 query["manufacturer"]访问功能代码中的参数。但老实说,它几乎与 request.Query["manufacturer"] 相同。 .

或者您可能必须遵循建议,将查询参数转换为类似 products/{productId} 的路由.

关于c# - 如何在Azure函数的自定义HTTP路由中指定查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54637165/

相关文章:

c# - 文本框自定义onPaint

c# - 为什么结构中的字段会丢失其值,而同一类中的字段却不会?

asp.net - 如何在本地测试 Azure Active Directory(回复 URL)

sql-server - 使用 BCP 将 CSV 文件导入到 Azure SQL Server

c# - 如何在 Blazor 服务器应用程序中正确计算 SignalR 消息?

c# - 使用不同的引用和框架编译.NET项目

python - 如何在 python Azure 函数中使用 ZBar 库?

c# - ASP.NET Core 日志记录过于详细

Azure Function 存储请求并稍后再次触发该函数

c# - wpf MvvM 命令数据上下文问题