在Azure Fucntion 2.x中使用C#进行HTTP请求的函数参数的模型绑定 [英] Model binding for function parameters for HTTP request with C# in Azure Fucntion 2.x

查看:48
本文介绍了在Azure Fucntion 2.x中使用C#进行HTTP请求的函数参数的模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是通过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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆