如何识别用于调用Azure功能的HTTP方法(动词)

问题描述:

从Azure门户,我们可以轻松创建功能应用程序。 一旦功能应用程序被创建,我们可以添加功能的应用程序。如何识别用于调用Azure功能的HTTP方法(动词)

就我而言,在自定义模板中,我选择了C#,API & Webhooks,然后选择了通用Webhook C#模板。

从集成菜单中的HTTP标题标题下,有一个下拉框,带有2个选择:所有方法和选定方法。然后我选择选定的方法,然后可以选择功能可以支持的HTTP方法。我希望我的函数支持GET,PATCH,DELETE,POST和PUT。

从C#的run.csx代码中,我该如何判断用什么方法调用方法?我希望能够根据用于调用函数的HTTP方法在功能代码中采取不同的操作。

这可能吗?

谢谢你的帮助。

回答我自己的问题......您可以检查HttpRequestMessage的Method属性,该属性的类型为HttpMethod

这里的MSDN文档:

HttpRequestMessage.Method物业

获取或设置HTTP请求消息使用的HTTP方法。

  • 名字空间:System.Net.Http
  • 装配:System.Net.Http(在System.Net.Http.dll

和快速样品:

#r "Newtonsoft.Json" 

using System; 
using System.Net; 
using Newtonsoft.Json; 

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log) 
{ 
    log.Info($"Webhook was triggered!"); 

    if (req.Method == HttpMethod.Post) 
    { 
     log.Info($"POST method was used to invoke the function ({req.Method})"); 
    } 
    else if (req.Method == HttpMethod.Get) 
    { 
     log.Info($"GET method was used to invoke the function ({req.Method})"); 
    } 
    else 
    { 
     log.Info($"method was ({req.Method})");  
    } 
}