使用model作为参数调用azure函数时出错

时间:2017-10-30 21:22:50

标签: azure azure-functions

我一直收到错误:

Exception while executing function: Functions.InsertItem -> Exception binding parameter 'newItem' -> No value was provided for parameter 'newItem' 

调用下面的azure函数时。

我实际上是使用天蓝色的门户来开发,没有VS.

public static async Task<HttpResponseMessage> Run( HttpRequestMessage req,
[HttpTrigger(AuthorizationLevel.Function, "POST")] Item newItem)
{                  
     return newItem == null
         ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass an item in the request body")
         : req.CreateResponse(HttpStatusCode.OK, "Hello " + newItem.Name);
} 

public class Item
{  
    public string Name { get; set; }    
}

知道出了什么问题吗?谢谢!

2 个答案:

答案 0 :(得分:1)

请将function.json更改为以下内容:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "newItem",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

newItem不应该有单独的条目,只需将其声明为触发器。

答案 1 :(得分:0)

例如,如果我们想发布一个JSON对象来运行如下

{
  "Item":
   { 
      "Name" : "test details"
   }
}

然后必须如下更新函数,如果要进行显式转换或在代码中使用动态对象来反序列化请求,则需要使用JSON反序列化器。

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");


    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

    // Set name to query string or body data
    string name = data.Item?.Name;

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

public class Item
{
    public string Name{get;set;}
}

以下是function.json

的绑定详细信息
{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}