Home > Back-end >  Azure Function says it cannot bind parameter
Azure Function says it cannot bind parameter

Time:07-11

I have some Azure Functions like this. The message I get is:

The 'GetTodoById' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'GetTodoById'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'id' to type String.

I don't understand why it says that! Id is defined a string as seen in my class below. And items is just some global variable for practice like this defined:

private static List<Todo> items = new List<Todo>();

So what is its problem?

public class Todo
{
    public string Id { get; set; } = Guid.NewGuid().ToString("n");
    public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
    public string TaskDescription { get; set; }
    public bool IsCompleted { get; set; }
}


[FunctionName("GetTodoById")]
public static IActionResult GetTodoById([HttpTrigger(AuthorizationLevel.Anonymous, "get", "todo/{id}")] HttpRequest request, ILogger logger, string id)
{
    var todo = items.FirstOrDefault(t => t.Id == id);
    if (todo == null)
    {
        return new NotFoundResult();
    }

    return new OkObjectResult(todo);
}

CodePudding user response:

You are using wrong overload of httpTrigger attribute

Try to use [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "todo/{id}")]

  • Related