如何自定义Azure功能URL

时间:2018-01-31 14:20:44

标签: azure azure-functions

刚开始使用azure函数。我将它用作物联网设备的httptrigger。

我正在尝试设置一个适用于来自多个物联网设备的httptrigger请求的功能 - 所以我不必为每个设备设置一个功能。理想情况下,在我的c#文件中,我会有这样的东西:

DeviceClient deviceClient;
string iotHubUri = "myhub";
string deviceName = "something dynamic here that changes with device";
string deviceKey = "something dynamic here that changes with device";

然后,我想让我的函数url看起来像这样:

"https://<functionapp>.azurewebsites.net/api/<function>/{device_id}?code=xxxxx"

其中device_id是IoT设备ID。

我不确定如何首先将c#文件中的引用设置为动态,以及如何使url看起来像我想要的那样。

一些帮助将不胜感激。感谢

2 个答案:

答案 0 :(得分:4)

路由参数恰好是HTTP触发器。您的触发器定义应如下所示:

"bindings": [
  {
    "type": "httpTrigger",
    "route": "something/{deviceid}",
    "name": "request",
    // possibly other parameters
  }
],

如果您使用预编译的C#函数,则可以通过属性属性执行相同的操作,例如

public static IActionResult Run(
    [HttpTrigger(AuthorizationLevel.Function, "GET", Route = "something/{deviceid}")] 
    HttpRequest request,
    string deviceid)
{
    // do something based on device id
}

答案 1 :(得分:0)

自定义您的Azure功能路线

要自定义您的网址,您可以使用两个文件,其中一个定义了函数function.jsonhost.json

function.json后缀

function.json中,您可以定义基本URL,并将"route": "yourbaseurl"属性添加到绑定数组。会导致类似这样的结果

{
  "scriptFile": "../dist/server.js",
  "disabled": false,
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "cool", ///// This is the line that you have to add /////
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

host.json前缀

host.json文件上,您可以为该路由添加前缀,假设您想添加一个精美的名称组...

因此您的文件应该看起来像这样

{
  "version": "2.0",
  "extensions": {
    "http": {    
        "routePrefix": "prefix" ///// here is that you include your prefix route /////
    }
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  }
}

我只想要一个字或一个字

如果您只想在路线中留下一个单词,请离开host.json "routePrefix": ""function.json "route": "therouteyouwant"或像"route": ""一样空。