C# 从windows窗体调用.NET core REST api以发送包括文件在内的数据

C# 从windows窗体调用.NET core REST api以发送包括文件在内的数据,c#,asp.net-core,C#,Asp.net Core,我有一个使用此模型的.NET核心Web Api: public class UserForRegisterDto { [Required] public string UserName { get; set; } [Required] public string Name { get; set; } [Required] public string LastName { get; set; } [Required] pu

我有一个使用此模型的.NET核心Web Api:

public class UserForRegisterDto
{
    [Required]
    public string UserName { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]      
    public string Password { get; set; }
    [Required]
    public string PhoneNumber { get; set; }
    [Required]
    public string MobilePhone { get; set; }

    [Required]
    public IFormFile Photo { get; set; }
}
另一方面,我有一个使用.NET 4的windows窗体应用程序,我尝试使用以下类和代码向api发送数据:

public class UserForRegisterDto
{
    public string UserName { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public string PhoneNumber { get; set; }
    public string MobilePhone { get; set; }
    public byte[] Photo { get; set; }
}
发送这些数据的代码是:

List<UserForRegisterDto> registrationList = Mapper.Map<List<UserForRegisterDto>>(usersList);
AuthenticatedHttpClientHandler clientHandler = new AuthenticatedHttpClientHandler(user.Id, Uow, _refreshTokenService);
clientHandler.UseDefaultCredentials = true;
HttpClient client = new HttpClient(clientHandler);
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["appUrl"].ToString());
StringContent content = new StringContent(JsonConvert.SerializeObject(registrationList), Encoding.UTF8, "application/json");


HttpResponseMessage response = await client.PostAsync($"api/users/{user.Id}/users/PostGasStationUsers/{ConfigurationManager.AppSettings["appId"].ToString()}", content);
List registrationList=Mapper.Map(usersList);
AuthenticatedHttpClientHandler-clientHandler=新的AuthenticatedHttpClientHandler(user.Id,Uow,_refreshTokenService);
clientHandler.UseDefaultCredentials=true;
HttpClient=新的HttpClient(clientHandler);
client.BaseAddress=新Uri(ConfigurationManager.AppSettings[“appUrl”].ToString());
StringContent=newStringContent(JsonConvert.SerializeObject(registrationList),Encoding.UTF8,“application/json”);
HttpResponseMessage response=await client.PostSync($“api/users/{user.Id}/users/PostGassStationUsers/{ConfigurationManager.AppSettings[“appId”].ToString()}”,内容);

我需要使用另一种数据类型,而不是可以映射到IFormFile数据的字节数组,以及如何做到这一点。

使用
MultipartFormDataContent
传输二进制数据

var content = new MultipartFormDataContent();

for (int i = 0; i < registrationList.Count; i++)
{
    UserForRegisterDto registration = registrationList[i];

    content.Add(new StringContent(registration.LastName), $"model[{i}].LastName");
    content.Add(new StringContent(registration.MobilePhone), $"model[{i}].MobilePhone");
    content.Add(new StringContent(registration.Name), $"model[{i}].Name");
    content.Add(new StringContent(registration.Password), $"model[{i}].Password");
    content.Add(new StringContent(registration.PhoneNumber), $"model[{i}].PhoneNumber");
    content.Add(new StringContent(registration.UserName), $"model[{i}].UserName");
    content.Add(new ByteArrayContent(registration.Photo), $"model[{i}].Photo", "photo.jpeg");
}

HttpResponseMessage response = await client.PostAsync($"api/users/{user.Id}/users/PostGasStationUsers/{ConfigurationManager.AppSettings["appId"].ToString()}", content);
[HttpPost]
public IActionResult PostGasStationUsers(List<UserForRegisterDto> model)