public override void OnActionExecuting(HttpActionContext actionContext) { String msgError = String.Empty; bool isValidate = true; System.Web.Http.ModelBinding.ModelStateDictionary model = new System.Web.Http.ModelBinding.ModelStateDictionary(); try { PaymentRequestDTO paymentRepresentationModel = new PaymentRequestDTO(); CardDetailsRequestDTO cardRepresentationModel = new CardDetailsRequestDTO(); PaymentConfirmationRequestDTO paymentConfRepresentationModel = new PaymentConfirmationRequestDTO(); try { paymentRepresentationModel = actionContext.ActionArguments["req"] as PaymentRequestDTO; } catch { paymentRepresentationModel = null; }; try { paymentConfRepresentationModel = actionContext.ActionArguments["req"] as PaymentConfirmationRequestDTO; } catch { paymentConfRepresentationModel = null; }; try { cardRepresentationModel = actionContext.ActionArguments["req"] as CardDetailsRequestDTO; } catch { cardRepresentationModel = null; }; model = actionContext.ModelState; msgError = "*** BAD REQUEST *** (" + actionContext.ActionDescriptor.ActionName + ") "; // Test BAD REQUEST (400) if (!model.IsValid) { msgError += string.Join(",", model.Values.Where(e => e.Errors.Count > 0) .SelectMany(e => e.Errors) .Select(e => e.ErrorMessage) .ToArray()); msgError += string.Join(",", model.SelectMany(e => e.Value.Errors) .Where(e => e.Exception != null) .Select(e => e.Exception.Message) .ToArray()); } else { if (paymentRepresentationModel != null) { isValidate = ((paymentRepresentationModel.Amount != 0) && (paymentRepresentationModel.bearer.Any()) && (paymentRepresentationModel.CardNumber.Any()) && (paymentRepresentationModel.CvvCode != 0) /*Add more validation*/); if (isValidate) { try { string jsonFormat = JsonConvert.SerializeObject(paymentRepresentationModel); } catch (JsonSerializationException e) { msgError += "Erreur :" + e.Message + ". Champs manquants"; isValidate = false; } } else { msgError += "Format JSON erroné"; } } if (paymentConfRepresentationModel != null) { isValidate = ((paymentRepresentationModel.Amount != 0) && (paymentConfRepresentationModel.bearer.Any()) && (paymentConfRepresentationModel.CardNumber.Any()) && (paymentConfRepresentationModel.CvvCode != 0) /*Add more validation*/); if (isValidate) { try { string jsonFormat = JsonConvert.SerializeObject(paymentConfRepresentationModel); } catch (JsonSerializationException e) { msgError += "Erreur :" + e.Message + ". Champs manquants"; isValidate = false; } } else { msgError += "Format JSON erroné"; } } if (cardRepresentationModel != null && paymentRepresentationModel == null) // PaymentRepresentation comprend CardRepresentation + Amount + Currency //==> On considere un CardRepresentationEntity uniquement si la requete n'a pas ete identifiee comme PaymentRepresentation { isValidate = ((paymentRepresentationModel.bearer.Any()) && (paymentRepresentationModel.CardNumber.Any()) || (paymentRepresentationModel.CvvCode != 0) /*Add more validation*/); if (isValidate) { try { string jsonFormat = JsonConvert.SerializeObject(cardRepresentationModel); } catch (JsonSerializationException e) { msgError += "Erreur :" + e.Message + ". Champs manquants"; isValidate = false; } } else { msgError += "Format JSON erroné"; } } } if (!model.IsValid || !isValidate) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent(msgError), ReasonPhrase = Utils.CleanNewLine(msgError) }); } } catch (HttpResponseException ex) { //LoggerService.Instance.LogFatal("RequestFilter.OnActionExecuting", "Model non valide," + msgError); throw ex; } catch (Exception ex) { //LoggerService.Instance.LogFatal("RequestFilter.OnActionExecuting", "Erreur de traitement: " + ex.Message); throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent(ex.Message), ReasonPhrase = Utils.CleanNewLine(msgError) }); } base.OnActionExecuting(actionContext); }