Search code examples
c#asp.netasp.net-web-apihttpclientflurl

Posting complex Class using Flurl


First, I have a 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; }
}

Secondly, I have a Web API 4.0 action method:

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

}

Problem: Am trying to post an instance of EnrolleeViewModel class to the Web API action, But this only works when Passport property of the class has no value.

Am posting using Flurl with the below code:

            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);
            }

Solution

  • Turns out this is from ASP Web API configuration limit. I simple added the below setting to web.config

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

    The maxRequestLength="10000" solved it for me