使用 Flurl 发布复杂的 Class

Posting complex Class using Flurl

首先,我有一个 class:

public class EnrolleeViewModel : EntityBase
{
        public string ID { get; set; }
        public long TenantID { get; set; }
        public string EnrolleeNo { get; set; }
        public byte[] Passport { get; set; }
        public string EnrolledBy { get; set; }
        public string SystemName { get; set; }
        public string MacAddress { get; set; }
        public string EnrolleeType { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string OtherName { get; set; }
        public string Gender { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public int? Age { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public List<EnrolleeFingerViewModel> enrolleeFingers { get; set; }
}

其次,我有一个Web API 4.0操作方法:

[HttpPost]
[Route(nameof(PostEnrollmentForStudent))]
public async Task<IHttpActionResult> PostEnrollmentForStudent(EnrolleeViewModel model)
{

}

问题: 我正在尝试 post EnrolleeViewModel class 的实例到 Web API 操作,但这仅在 Passport 属性 的 class没有价值。

正在 post 使用 Flurl 和以下代码:

            try
            {
                EnrolleeViewModel model = new EnrolleeViewModel()
                {
                    UserID = GlobalResources.UserViewModel.ID,
                    MacAddress = Utils.GetMacAddress(),
                    SystemName = Environment.MachineName,
                    EnrolleeNo = txtRegistrationNo.Text,
                    EnrolleeType = EnrolleeType.STUDENT.ToString(),
                    Passport = AppConverter.ConvertImageToByte(clagPassportEnrollment1.passportPictureBox.Image),
                    IsActive = true,
                    IsDeleted = false,
                    LastName = LastName,
                    FirstName = FirstName,
                    OtherName = MiddleName,
                    Phone = PhoneNo,
                    Email = Email,
                    enrolleeFingers = new List<EnrolleeFingerViewModel>(),
                };

                List<EnrolleeFingerViewModel> fingers = new List<EnrolleeFingerViewModel>();
                foreach (var finger in clagFingerEnrollment1.enrolledFingers)
                {
                    model.enrolleeFingers.Add(new EnrolleeFingerViewModel()
                    {
                        FingerData = finger.FingerData,
                        HandType = finger.HandType,
                        FingerType = finger.FingerType,
                        FingerIndex = finger.FingerIndex,
                        FingerRawImage = finger.FingerRawImage,
                        SDKType = finger.SDKType,
                    });
                }

                var url = Url.Combine(Properties.Settings.Default.EndPointBase, Properties.Settings.Default.EndPointCreateEnrollment);

                IFlurlResponse response = url.AllowAnyHttpStatus().PostJsonAsync(model).Result;
                if (response.ResponseMessage.IsSuccessStatusCode)
                {
                    MessageBox.Show(this, $"{txtRegistrationNo.Text} enrolled successfully", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ResetData();
                }
                else if(response.StatusCode == (int)HttpStatusCode.BadRequest)
                {
                    dynamic result = response.GetJsonAsync<ExpandoObject>().Result;
                    throw new Exception(result.Message ?? "Request not successful");
                }
            }
            catch (FlurlHttpException x)
            {
                HandleFormError(x);
            }
            catch (Exception x)
            {
                HandleFormError(x);
            }

原来这是来自 ASP Web API 配置限制。我简单地将以下设置添加到 web.config

    <system.web>
        <httpRuntime maxRequestLength="10000"/>
    </system.web>

maxRequestLength="10000"帮我解决了