如何使用 Azure Functions 命名 API 路由“...admin...”?
How to name API route "...admin..." using Azure Functions?
我正在将组件从 Web 服务移动到 Azure Functions。我想保持 API 端点不变。我有一个管理控制器,它有这样的端点:
/admin/Heartbeat
我对应的实现是这样的:
[FunctionName("Heartbeat")]
public static async Task<IActionResult> RunHeartbeat(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "admin/Heartbeat" )] HttpRequest req,
ILogger log)
{
...
}
如果任何路由以 "admin" 开头,我会收到此错误消息:
The specified route conflicts with one or more built in routes.
几乎没有关于此内置限制的信息 - 或者我找不到它。在 GitHub (thread 1, thread 2) 上有几次提及,但没有人对这个问题有合适的解决方案。
解决方法是重命名路线 - 这正是我想要避免的。
是否有任何解决方案允许在路由中使用 "admin"?
看起来路由中的 admin
是严格保留给 azure 函数 运行time 主机的。
目前,任何以 admin
开头的路由都有这个问题。
Admin API 由函数主机公开:
GET https://<functionappname>.azurewebsites.net/admin/host/status
回复:
{
"id": "bad1ecf31b47-2137340777",
"state": "Running",
"version": "2.0.1.0",
"versionDetails": "2.0.1.0-beta1 Commit hash: N/A"
}
更多信息:https://github.com/Azure/azure-functions-host/wiki/Admin-API
解决方法
您可以在函数代码中将路由名称更改为v2/admin/Heartbeat
。
示例:
[FunctionName("ExampleAdminRoute")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v2/admin/Heartbeat")]HttpRequest req, ILogger log)
{
//your admin function code
}
然后,您可以向路由添加代理(有关 azure 函数代理的更多信息 here)。代理存储在 proxies.json
中。
确保将此文件复制到构建输出目录,即包含在 .csproj 文件中。
示例proxies.json
:
{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"AdminProxy": {
"matchCondition": {
"methods": [ "GET" ],
"route": "/api/admin/Heartbeat"
},
"backendUri": "https://localhost/api/v2/admin/Heartbeat"
}
}
}
现在,当您 运行 函数应用程序时,您应该看到如下控制台输出:
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
Listening on http://0.0.0.0:7071/
Hit CTRL-C to exit...
Http Functions:
AdminProxy: http://localhost:7071/api/admin/Heartbeat
ExampleAdminRoute: http://localhost:7071/api/v2/admin/Heartbeat
任何到达 api/admin/Heartbeat
的请求都将自动重定向到 api/v2/admin/Heartbeat
。
代理足够灵活,可以支持带有参数等的不同请求 (GET/POST)
我正在将组件从 Web 服务移动到 Azure Functions。我想保持 API 端点不变。我有一个管理控制器,它有这样的端点:
/admin/Heartbeat
我对应的实现是这样的:
[FunctionName("Heartbeat")]
public static async Task<IActionResult> RunHeartbeat(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "admin/Heartbeat" )] HttpRequest req,
ILogger log)
{
...
}
如果任何路由以 "admin" 开头,我会收到此错误消息:
The specified route conflicts with one or more built in routes.
几乎没有关于此内置限制的信息 - 或者我找不到它。在 GitHub (thread 1, thread 2) 上有几次提及,但没有人对这个问题有合适的解决方案。
解决方法是重命名路线 - 这正是我想要避免的。
是否有任何解决方案允许在路由中使用 "admin"?
看起来路由中的 admin
是严格保留给 azure 函数 运行time 主机的。
目前,任何以 admin
开头的路由都有这个问题。
Admin API 由函数主机公开:
GET https://<functionappname>.azurewebsites.net/admin/host/status
回复:
{
"id": "bad1ecf31b47-2137340777",
"state": "Running",
"version": "2.0.1.0",
"versionDetails": "2.0.1.0-beta1 Commit hash: N/A"
}
更多信息:https://github.com/Azure/azure-functions-host/wiki/Admin-API
解决方法
您可以在函数代码中将路由名称更改为v2/admin/Heartbeat
。
示例:
[FunctionName("ExampleAdminRoute")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v2/admin/Heartbeat")]HttpRequest req, ILogger log)
{
//your admin function code
}
然后,您可以向路由添加代理(有关 azure 函数代理的更多信息 here)。代理存储在 proxies.json
中。
确保将此文件复制到构建输出目录,即包含在 .csproj 文件中。
示例proxies.json
:
{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"AdminProxy": {
"matchCondition": {
"methods": [ "GET" ],
"route": "/api/admin/Heartbeat"
},
"backendUri": "https://localhost/api/v2/admin/Heartbeat"
}
}
}
现在,当您 运行 函数应用程序时,您应该看到如下控制台输出:
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
Listening on http://0.0.0.0:7071/
Hit CTRL-C to exit...
Http Functions:
AdminProxy: http://localhost:7071/api/admin/Heartbeat
ExampleAdminRoute: http://localhost:7071/api/v2/admin/Heartbeat
任何到达 api/admin/Heartbeat
的请求都将自动重定向到 api/v2/admin/Heartbeat
。
代理足够灵活,可以支持带有参数等的不同请求 (GET/POST)