如何识别用于调用 Azure 函数的 HTTP 方法(动词)
How to identify the HTTP method (verb) used to invoke an Azure Function
从 Azure 门户,我们可以轻松创建函数应用程序。
Function App创建完成后,我们就可以给app添加功能了。
在我的例子中,我从自定义模板 selecting C#、API 和 Webhooks,然后 selecting Generic Webhook C# 模板。
在 Integrate 菜单中,在 HTTP Header 标题下,有一个包含 2 selections 的下拉框:All Methods 和 Selected方法。然后我选择 Selected Methods,然后可以选择 select 该函数可以支持哪些 HTTP 方法。我希望我的函数支持 GET、PATCH、DELETE、POST 和 PUT。
在 C# run.csx 代码中,我如何知道调用方法时使用了哪个方法?我希望能够根据用于调用函数的 HTTP 方法在函数代码中执行不同的操作。
这可能吗?
感谢您的帮助。
回答我自己的问题...您可以检查 HttpRequestMessage
的方法 属性,它是 HttpMethod
.
类型
这是 MSDN 文档:
HttpRequestMessage.Method
Property
Gets or sets the HTTP method used by the HTTP request message.
- 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 门户,我们可以轻松创建函数应用程序。 Function App创建完成后,我们就可以给app添加功能了。
在我的例子中,我从自定义模板 selecting C#、API 和 Webhooks,然后 selecting Generic Webhook C# 模板。
在 Integrate 菜单中,在 HTTP Header 标题下,有一个包含 2 selections 的下拉框:All Methods 和 Selected方法。然后我选择 Selected Methods,然后可以选择 select 该函数可以支持哪些 HTTP 方法。我希望我的函数支持 GET、PATCH、DELETE、POST 和 PUT。
在 C# run.csx 代码中,我如何知道调用方法时使用了哪个方法?我希望能够根据用于调用函数的 HTTP 方法在函数代码中执行不同的操作。
这可能吗?
感谢您的帮助。
回答我自己的问题...您可以检查 HttpRequestMessage
的方法 属性,它是 HttpMethod
.
这是 MSDN 文档:
HttpRequestMessage.Method
PropertyGets or sets the HTTP method used by the HTTP request message.
- 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})");
}
}