/// <summary> /// Initializes a new instance of the <see cref="ActionMethodModelStateValidatedLogEntry" /> class. /// </summary> /// <param name="method">The method being invoked.</param> /// <param name="request">The request being executed on the method.</param> /// <param name="modelState">the model dictionary created by the controller.</param> public ActionMethodModelStateValidatedLogEntry( IGraphMethod method, IDataRequest request, InputModelStateDictionary modelState) : base(LogEventIds.ControllerModelValidated) { this.PipelineRequestId = request.Id; this.ControllerName = method.Parent.ObjectType?.FriendlyName(true) ?? method.Parent.Name; this.ActionName = method.Name; this.FieldPath = method.Route.Path; this.ModelDataIsValid = modelState.IsValid; _shortControllerName = method.Parent.ObjectType?.FriendlyName() ?? method.Parent.Name; this.ModelItems = null; if (modelState.Values != null && modelState.Values.Any()) { var entries = new List <ModelStateEntryLogItem>(); foreach (var item in modelState.Values) { if (item.ValidationState == InputModelValidationState.Invalid) { entries.Add(new ModelStateEntryLogItem(item)); } } this.ModelItems = entries; } }
private void ValidateModelDictionaryToLogEntry( IGraphMethod graphMethod, IGraphFieldRequest fieldRequest, InputModelStateDictionary dictionary, ActionMethodModelStateValidatedLogEntry logEntry) { Assert.AreEqual(fieldRequest.Id, logEntry.PipelineRequestId); Assert.AreEqual(graphMethod.Parent.ObjectType.FriendlyName(true), logEntry.ControllerName); Assert.AreEqual(graphMethod.Name, logEntry.ActionName); Assert.AreEqual(dictionary.IsValid, logEntry.ModelDataIsValid); foreach (var kvp in dictionary) { var itemResult = kvp.Value; if (itemResult.ValidationState == InputModelValidationState.Invalid) { var entryResult = logEntry.ModelItems.Cast <ModelStateEntryLogItem>().First(x => itemResult.Name == x.Name); Assert.AreEqual(itemResult.Name, entryResult.Name); Assert.AreEqual(itemResult.ValidationState.ToString(), entryResult.ValidationState); var itemErrors = itemResult.Errors; var entryErrors = entryResult.Errors; if (itemErrors.Count == 0) { // log entry should skip the property if // no errors are recorded Assert.IsTrue(entryErrors == null); continue; } Assert.AreEqual(itemErrors.Count, entryErrors.Count); for (var i = 0; i < itemErrors.Count; i++) { var itemError = itemErrors[i]; var entryError = entryErrors[i] as ModelStateErrorLogItem; Assert.AreEqual(itemError.ErrorMessage, entryError.ErrorMessage); var itemException = itemError.Exception; var entryException = entryError.Exception as ExceptionLogItem; Assert.AreEqual(itemException == null, entryException == null); if (itemException != null) { Assert.AreEqual(itemException.Message, entryException.ExceptionMessage); Assert.AreEqual(itemException.StackTrace, entryException.StackTrace); Assert.AreEqual(itemException.GetType().FriendlyName(true), entryException.TypeName); } } } } }
/// <inheritdoc /> public virtual void ActionMethodModelStateValidated(IGraphMethod action, IDataRequest request, InputModelStateDictionary modelState) { if (!this.IsEnabled(LogLevel.Trace)) { return; } var entry = new ActionMethodModelStateValidatedLogEntry(action, request, modelState); this.LogEvent(LogLevel.Trace, entry); }
/// <summary> /// Returns an negative result, indicating the data supplied on the request was bad or /// otherwise not usable by the controller method. /// </summary> /// <param name="modelState">The model state with its contained validation failures.</param> /// <returns>IGraphActionResult.</returns> protected virtual IGraphActionResult BadRequest(InputModelStateDictionary modelState) { return(new BadRequestGraphActionResult(modelState)); }
/// <summary> /// Initializes a new instance of the <see cref="BadRequestGraphActionResult"/> class. /// </summary> /// <param name="modelState">State of the model.</param> public BadRequestGraphActionResult(InputModelStateDictionary modelState) { _modelState = modelState; }