如何在Azure Functions中使用HttpTrigger进行ModelBinding?

时间:2017-06-20 18:15:00

标签: azure model-binding azure-functions

我需要创建一个响应HTTP POST的Azure功能,并利用集成的模型绑定。

如何修改此

        Dim di As New DirectoryInfo(Path), fiArr As FileInfo() = di.GetFiles()
        Dim strplik As FileInfo
        For Each strplik In fiArr
        'read strplik from file
        next strplik

以这样的方式,我的客户端POST的对象,我有正常的ASP.NET样式模型绑定?

2 个答案:

答案 0 :(得分:4)

根据HTTP trigger from code的文档,您只需接受自己的对象:

  

对于自定义类型(例如POCO),函数将尝试解析   请求体作为JSON来填充对象属性。

public class MyModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

[FunctionName("TokenPolicy")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "TokenPolicy/{IssuerID}/{SpecificationID}")]MyModel myObj, string IssuerID, string specificationID, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request. TokenPolicy");


    // Do something your your object

    return new HttpResponseMessage(HttpStatusCode.OK);
}

答案 1 :(得分:2)

您可以使用自定义类型,而不是使用HttpRequestMessage参数。绑定将尝试将请求主体解析为JSON,并在调用函数之前填充该对象。这里有一些细节:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook#http-trigger