c# - 使用 HttpTrigger 属性在 Azure 函数中自定义路由

标签 c# function azure

我有一个已编译的 Azure 函数,我想使用 HttpTrigger 属性定义自定义路由。

我的代码如下所示:

public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "qotd/{format}")]HttpRequestMessage req, string format, TraceWriter log)
{
  var quote = GetRandomQuote();
  if (format == "json")
  {
     return req.CreateResponse(HttpStatusCode.OK, quote, "application/json");
  }
  else
  {
     var strQuote = quote.Text + Environment.NewLine + quote.Author;
     return req.CreateResponse(HttpStatusCode.OK, strQuote, "text/plain");
  }
}

当我这样调用它时: localhost:7071/api/qotd/json 我得到 404。

当我这样调用它时: localhost:7071/api/qotd/?format=json 那么函数就成功了。

如果我这样调用它: 本地主机:7071/api/qotd/ 我收到一个相当严重的错误:

"Exception while executing function: QotDFunction -> Exception binding parameter 'format' -> Binding data does not contain expected value 'format'..."

如何在 HttpTrigger 的 Route 参数中定义绑定(bind),以便我可以像这样调用我的函数:

localhost:7071/api/qotd - 参数格式的默认值

localhost:7071/api/qotd/json - 将“json”作为格式值传递?

对于路线,我也尝试了“qotd/{format:alpha?}”,但得到了相同的结果。

最佳答案

事实证明,我必须明确地重建该项目。重建它迫使 VS 创建一个具有正确路由的新 function.json 文件。如果不这样做,function.json 仍然保留旧的默认路由。

关于c# - 使用 HttpTrigger 属性在 Azure 函数中自定义路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48305132/

相关文章:

c# - 是否存在任何NON-GPL/AGPL病毒扫描库?

javascript - 如何使用鼠标位置使窗口滚动?

asp.net-mvc - IIS 不允许 MVC 应用程序控制 Azure 上的错误消息

c# - Microsoft Graph API,文件夹资源类型

c# - 如何进行一系列异步调用,然后使用第一个线程返回的值?

c# - Kendo 网格、虚拟滚动、页面大小和聚合

javascript - 为什么我的函数 "donethingy"被视为 "not declared"?

javascript - HTML/JavaScript : How to run a loop while running other code?

mysql - 连接到 azure 的Mysql服务器

azure - Logstash 无法向 Elasticsearch Master 发送加入请求