如何识别用于调用Azure函数的HTTP方法(动词) [英] How to identify the HTTP method (verb) used to invoke an Azure Function
问题描述
从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和Webhooks,然后选择通用Webhook C#模板.
In my case, from the custom templates, I'm selecting C#, API & Webhooks, and then selecting the Generic Webhook C# template.
在集成"菜单的" HTTP标题"标题下,有一个下拉框,其中有两个选项:所有方法"和选定方法".然后,我选择 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文档:
-
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方法(动词)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 命名空间: