问题描述
我知道我可以像这样使用 url 参数:
I know that I can use url parameters like this:
"myfunction?p=one&p2=two"
在代码中变成
request.query.p = "one";
但我更喜欢这样(表达路由风格):
but I would prefer to get it like this (express routing style):
"myfunction/:myvalue"
并使用此网址:
/myfunction/nine
在代码中变成这样:
request.params.myvalue = "nine"
但我似乎找不到如何在 Azure Functions 中配置这样的路由路径,有什么想法或文档吗?
but I can't seem to find how to configure a route path like that in Azure Functions, any ideas or docs?
推荐答案
我们已经为 Azure Functions 中的 HTTP 触发器提供了路由支持.您现在可以添加遵循 ASP.NET Web API 路由命名语法的路由属性.(您可以直接通过 Function.json 或门户 UX 进行设置)
We've shipped route support for HTTP Triggers in Azure Functions. You can now add a route property which follows ASP.NET Web API route naming syntax. (You can set it directly via the Function.json or via the portal UX)
"route": "node/products/{category:alpha}/{id:guid}"
函数.json:
{
"bindings": [
{
"type": "httpTrigger",
"name": "req",
"direction": "in",
"methods": [ "post", "put" ],
"route": "node/products/{category:alpha}/{id:guid}"
},
{
"type": "http",
"name": "$return",
"direction": "out"
},
{
"type": "blob",
"name": "product",
"direction": "out",
"path": "samples-output/{category}/{id}"
}
]
}
.NET 示例:
public static Task<HttpResponseMessage> Run(HttpRequestMessage request, string category, int? id,
TraceWriter log)
{
if (id == null)
return req.CreateResponse(HttpStatusCode.OK, $"All {category} items were requested.");
else
return req.CreateResponse(HttpStatusCode.OK, $"{category} item with id = {id} has been requested.");
}
NodeJS 示例:
module.exports = function (context, req) {
var category = context.bindingData.category;
var id = context.bindingData.id;
if (!id) {
context.res = {
// status: 200, /* Defaults to 200 */
body: "All " + category + " items were requested."
};
}
else {
context.res = {
// status: 200, /* Defaults to 200 */
body: category + " item with id = " + id + " was requested."
};
}
context.done();
}
<罢工>今天,您必须使用 Azure API 管理之类的服务来自定义您的路线.
Today, you'd have to use a service like Azure API Management to customize your routes.
正在进行 PR 以将自定义路由添加到 Azure Functions 本身.该团队希望它将在下一个版本中登陆.https://github.com/Azure/azure-webjobs-sdk-script/拉/490
There is a PR in progress to add custom routes to Azure Functions itself. The team is hoping it will land in the next release. https://github.com/Azure/azure-webjobs-sdk-script/pull/490
注意:我是 Azure Functions 团队的 PM
Note: I'm a PM on the Azure Functions team
这篇关于如何在 Azure Functions 中进行路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2