在Azure Fucntion 2.x中使用C#进行HTTP请求的函数参数的模型绑定 [英] Model binding for function parameters for HTTP request with C# in Azure Fucntion 2.x
问题描述
下面是通过Visual Studio通过C#编写的Azure Functions.问题是req.Form
用于创建RequestDto
对象,如下所示:
Below is Azure Functions via C# with visual studio. The problem is that req.Form
is used to create RequestDto
object like below:
public class Function1
{
[FunctionName("Token")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
HttpRequest req)
{
var reqDto = new RequestDto
{
UserName = req.Form["username"],
Password = req.Form["password"],
ClientId = req.Form["client_id"],
ClientSecret = req.Form["client_secret"]
};
...
}
}
是否可以使用通过ASP.NET CORE 2.x自动填充RequestDto
的模型绑定,如下所示?
Is it possible to use model binding that populates RequestDto
automatically with ASP.NET CORE 2.x like below?
[Route("api/[controller]")]
[ApiController]
public class ConnectController : Controller
{
[HttpPost("token")]
[Consumes("application/x-www-form-urlencoded")]
public async Task<IActionResult> Token([FromForm]RequestDto request)
{
...
}
}
public class RequestDto
{
[FromForm(Name="client_id")]
public string ClientId { get; set; }
[FromForm(Name = "client_secret")]
public string ClientSecret { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
推荐答案
我认为您正在寻找的是自定义绑定,或者被Microsoft称为:Azure Functions/WebJobs绑定扩展.此处说明了如何执行此操作: https://github.com/Azure/WebJobsExtensionSamples
I think what you are looking for is a custom binding or as called by Microsoft: Azure Functions/WebJobs binding extension. How to do this is explained here: https://github.com/Azure/WebJobsExtensionSamples
这篇关于在Azure Fucntion 2.x中使用C#进行HTTP请求的函数参数的模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!