如何在Azure Functions中进行路由? [英] How can I do Routing in Azure Functions?

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

问题描述

我知道我可以使用以下网址参数:

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路由命名语法的route属性。 (您可以直接通过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}

Function.json:

Function.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-trigger-the-function


今天,您必须使用Azure API Management之类的服务来自定义路线。

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 / pull / 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

注意:我是一名PM Azure Functions团队

Note: I'm a PM on the Azure Functions team

这篇关于如何在Azure Functions中进行路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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