Azure Functions 模型绑定
Azure Functions model binding
我已经创建了一个 Azure Functions 并且我在本地运行它:
[FunctionName("HttpTriggerCSharpSet")]
public static async Task<HttpResponseMessage> Set([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] MyDocument req, TraceWriter log)
{
// ...
}
注意第一个参数是 MyDocument
而不是 HttpRequestMessage
。我在文档中读到这种方法应该有效,它看起来与 ASP.NET 模型绑定非常相似(无论如何在我看来)。 MyDocument
是一个只有 3 个属性的 POCO。
public class MyDocument
{
public string Name { get; set; }
public int ShoeSize { get; set; }
public decimal Balance { get; set; }
}
当我 POST 这样的函数时(我正在使用 Postman):
我收到一条错误消息:[8/8/2017 2:21:07 PM] Exception while executing function: Functions.HttpTriggerCSharpSet. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'req'. System.Net.Http.Formatting: No MediaTypeFormatter is available to read an object of type 'MyDocument' from content
(您也可以在上面的 Postman 屏幕截图中看到)
我试过 form-data 和 x-www-form-urlencoded 甚至来自 Postman 的 raw,每次都出现同样的错误。我也试过切换回 HttpRequestMessage
并使用 req.Content.ReadAsAsync<MyDocument>
,我得到了类似的错误。我是否错误地构建了我的 POST,或者我是否错误地编写了我的 Azure 函数。无论哪种情况,正确的方法是什么?
确保指定 header:
Content-Type: application/json
那么以下 body 应该适用于您的代码:
{
"Name": "myUserName",
"Balance": 123.0,
"ShoeSize": 30
}
我已经创建了一个 Azure Functions 并且我在本地运行它:
[FunctionName("HttpTriggerCSharpSet")]
public static async Task<HttpResponseMessage> Set([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] MyDocument req, TraceWriter log)
{
// ...
}
注意第一个参数是 MyDocument
而不是 HttpRequestMessage
。我在文档中读到这种方法应该有效,它看起来与 ASP.NET 模型绑定非常相似(无论如何在我看来)。 MyDocument
是一个只有 3 个属性的 POCO。
public class MyDocument
{
public string Name { get; set; }
public int ShoeSize { get; set; }
public decimal Balance { get; set; }
}
当我 POST 这样的函数时(我正在使用 Postman):
我收到一条错误消息:[8/8/2017 2:21:07 PM] Exception while executing function: Functions.HttpTriggerCSharpSet. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'req'. System.Net.Http.Formatting: No MediaTypeFormatter is available to read an object of type 'MyDocument' from content
(您也可以在上面的 Postman 屏幕截图中看到)
我试过 form-data 和 x-www-form-urlencoded 甚至来自 Postman 的 raw,每次都出现同样的错误。我也试过切换回 HttpRequestMessage
并使用 req.Content.ReadAsAsync<MyDocument>
,我得到了类似的错误。我是否错误地构建了我的 POST,或者我是否错误地编写了我的 Azure 函数。无论哪种情况,正确的方法是什么?
确保指定 header:
Content-Type: application/json
那么以下 body 应该适用于您的代码:
{
"Name": "myUserName",
"Balance": 123.0,
"ShoeSize": 30
}