如何识别用于调用 Azure 函数的 HTTP 方法(动词) [英] How to identify the HTTP method (verb) used to invoke an Azure Function

查看:11
本文介绍了如何识别用于调用 Azure 函数的 HTTP 方法(动词)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 Azure 门户,我们可以轻松创建函数应用程序.创建 Function App 后,我们可以向该应用添加功能.

From the Azure Portal, we can easily create Function Apps. Once the Function App is created, we can add functions to the app.

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

In my case, from the custom templates, I'm selecting C#, API & Webhooks, and then selecting the Generic Webhook C# template.

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

From the Integrate menu, under the HTTP Header heading, there is a drop down box to with 2 selections: All Methods and Selected Methods. I then choose Selected Methods, and then have the option to select which HTTP methods the function can support. I would like my function to support GET, PATCH, DELETE, POST and PUT.

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

From within the C# run.csx code, how can I tell what method was used to invoke the method? I would like to be able to take different actions within the function code based on the HTTP method that was used to invoke the function.

这可能吗?

感谢您的帮助.

推荐答案

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

Answering my own question... you can examine the HttpRequestMessage's Method property, which is of type HttpMethod.

这是 MSDN 文档:

Here's the MSDN documentation:

HttpMethod MSDN 文档

HttpRequestMessage.Method属性

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

Gets or sets the HTTP method used by the HTTP request message.

  • 命名空间:System.Net.Http
  • 程序集:System.Net.Http(在 System.Net.Http.dll 中)
  • Namespace: System.Net.Http
  • Assembly: System.Net.Http (in 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})");    
    }
}

这篇关于如何识别用于调用 Azure 函数的 HTTP 方法(动词)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆