How to identify the HTTP method (verb) used to invoke an Azure Function(如何识别用于调用 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:
HttpRequestMessage MSDN 文档
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
(inSystem.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 方法(动词)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何识别用于调用 Azure 函数的 HTTP 方法(动词)
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01