【问题标题】:Azure functions HttpTrigger, not using HttpMethods class in attributeAzure 函数 HttpTrigger,不在属性中使用 HttpMethods 类
【发布时间】:2020-12-13 21:51:05
【问题描述】:

为什么 AzureFunctions 中的函数需要使用

[HttpTrigger(AuthorizationLeve.Anonymous, "get")]

而不是

[HttpTrigger(AuthorizationLeve.Anonymous,  HttpMethods.Get)]

当然不能用

HttpMethods.Get.ToString()

因为语言不允许在属性中调用函数

并创建我自己的

public static class HttpMethodsStrings
{
     public const string Get = "get";
}

似乎是多余的,因为 .NET 已经提供了 HttpMethods。

【问题讨论】:

  • 如果您认为回复回答了您的问题,请将其标记为答案以帮助其他社区人员。
  • 嗯,很可能会创建常量,我希望 MS 在某处提供常量,但似乎没有,所以我等待接受。谢谢

标签: .net-core azure-functions


【解决方案1】:

您可以使用System.Net.WebRequestMethods.Http.X 常量代替硬编码字符串(“get”、“post”)。我已经把这段代码放在一起并调试了它,它工作正常。 同样的sn-p如下图-

【讨论】:

    【解决方案2】:

    您可以修改第二个示例以使用 nameof 运算符

    例如

    [HttpTrigger(AuthorizationLeve.Anonymous,  HttpMethods.Get)]
    

    变成

    [HttpTrigger(AuthorizationLeve.Anonymous,  nameof(HttpMethods.Get))]
    

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多