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

Yve*_*hon 4 c# azure azure-functions

从Azure门户,我们可以轻松创建功能应用程序.创建功能应用程序后,我们可以向应用程序添加功能.

就我而言,从自定义模板中,我选择C#,API和Webhooks,然后选择Generic Webhook C#模板.

在"集成"菜单的" HTTP标题"标题下,有一个下拉框,其中包含2个选项:"所有方法"和"所选方法".然后我选择Selected Methods,然后选择选择函数可以支持的HTTP方法.我希望我的函数支持GET,PATCH,DELETE,POST和PUT.

在C#run.csx代码中,如何判断使用哪种方法来调用该方法?我希望能够根据用于调用函数的HTTP方法在函数代码中执行不同的操作.

这可能吗?

谢谢您的帮助.

Yve*_*hon 9

回答我自己的问题......你可以检查一下HttpRequestMessage类型的方法属性HttpMethod.

这是MSDN文档:

HttpRequestMessage.Method 属性

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

  • 命名空间: System.Net.Http
  • 大会:( System.Net.HttpSystem.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})");    
    }
}
Run Code Online (Sandbox Code Playgroud)