如何在Azure函数的自定义HTTP路由中指定查询参数? [英] How to specify a query parameter in a custom HTTP route of an Azure Function?

查看:146
本文介绍了如何在Azure函数的自定义HTTP路由中指定查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure函数,我想设置一个自定义HTTP终结点.按照此答案问题,我最终得到这样的东西:

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函数的自定义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路由的内置限制,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并使用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函数的自定义HTTP路由中指定查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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