Some weeks ago, I tested how to convert an API using functions with special attention to DI (not shown in below example) and validation. This may not be a direct answer to your question, but it shows that plain model classes may be used.
public class MyRequestModel
{
[Display(Name = "Message")]
[Required, MinLength(3)]
public string Message { get; set; }
}
public static class MyHttpExample
{
[FunctionName(nameof(MyHttpExample))]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "test/{message}")] MyRequestModel req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var validationResults = new List<ValidationResult>();
if (!Validator.TryValidateObject(req, new ValidationContext(req, null, null), validationResults, true))
{
return new BadRequestObjectResult(validationResults);
}
var responseMessage = $"Hello, {req.Message}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
More information on Azure Functions binding expression patterns
https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-patterns
When reading the above doc, keep in mind that function.json is generated from annotations when using C#, but doc shows what's available. Generated function.json for the above example:
{
"generatedBy": "Microsoft.NET.Sdk.Functions-3.0.11",
"configurationSource": "attributes",
"bindings": [
{
"type": "httpTrigger",
"route": "test",
"methods": [
"get"
],
"authLevel": "function",
"name": "req"
}
],
"disabled": false,
"scriptFile": "../bin/MyFunctions.dll",
"entryPoint": "MyFunctions.MyHttpExample.Run"
}
See also https://stackoverflow.com/a/54489040/14072498
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…