如何在 Azure Functions 中使用 Http 触发器进行模型绑定?
How can I do ModelBinding with HttpTrigger in Azure Functions?
我需要创建一个响应 HTTP POST 并利用集成模型绑定的 Azure 函数。
我该如何修改这个
[FunctionName("TokenPolicy")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "TokenPolicy/{IssuerID}/{SpecificationID}")]HttpRequestMessage req, string IssuerID, string specificationID, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request. TokenPolicy");
// Fetching the name from the path parameter in the request URL
return req.CreateResponse(HttpStatusCode.OK, "data " + specificationID);
}
我的客户 POST 是对象,而我有正常的 ASP.NET 样式模型绑定?
您可以使用自定义类型来代替 HttpRequestMessage
参数。绑定将尝试将请求主体解析为 JSON 并在调用函数之前填充该对象。这里有一些细节:https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp#payload
根据 HTTP trigger from code 的文档,您可以简单地接受您自己的对象:
For a custom type (such as a POCO), Functions will attempt to parse
the request body as JSON to populate the object properties.
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);
}
我需要创建一个响应 HTTP POST 并利用集成模型绑定的 Azure 函数。
我该如何修改这个
[FunctionName("TokenPolicy")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "TokenPolicy/{IssuerID}/{SpecificationID}")]HttpRequestMessage req, string IssuerID, string specificationID, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request. TokenPolicy");
// Fetching the name from the path parameter in the request URL
return req.CreateResponse(HttpStatusCode.OK, "data " + specificationID);
}
我的客户 POST 是对象,而我有正常的 ASP.NET 样式模型绑定?
您可以使用自定义类型来代替 HttpRequestMessage
参数。绑定将尝试将请求主体解析为 JSON 并在调用函数之前填充该对象。这里有一些细节:https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp#payload
根据 HTTP trigger from code 的文档,您可以简单地接受您自己的对象:
For a custom type (such as a POCO), Functions will attempt to parse the request body as JSON to populate the object properties.
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);
}