我们已经为 Azure Functions 中的 HTTP 触发器提供了路由支持。您现在可以添加遵循 ASP.NET Web API 路由命名语法的路由属性。 (您可以直接通过 Function.json 或门户 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();
}
官方文档:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook#url-to-trigger-the-function
今天,您必须使用 Azure API 管理之类的服务来自定义您的路线。
正在进行 PR 以将自定义路由添加到 Azure Functions 本身。该团队希望它将在下一个版本中登陆。 https://github.com/Azure/azure-webjobs-sdk-script/pull/490
注意:我是 Azure Functions 团队的 PM