コード例 #1
0
        public void Test_ValidationFailedResult_ToStringEmpty()
        {
            // Arrange
            var modelState = new ModelStateDictionary();

            // Act
            var res = new ValidationFailedResult(modelState);
            var str = res.ToString();

            // Assert
            str.Should().Be("Validation Error (400): Model could not be validated.");
        }
コード例 #2
0
        /// <summary>
        /// Called when [action executing].
        /// </summary>
        /// <param name="context">The context.</param>
        /// <inheritdoc />
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                var validationFailedResult = new ValidationFailedResult(context.ModelState);

                if (_logMessage)
                {
                    var logger = (ILogger)context.HttpContext.RequestServices.GetService(typeof(ILogger));
                    logger?.LogInformation(validationFailedResult.ToString());
                }

                context.Result = validationFailedResult;
            }
        }
コード例 #3
0
        public void Test_ValidationFailedResult_ToString()
        {
            // Arrange
            var modelState = new ModelStateDictionary();

            modelState.AddModelError("TestKey1", "TestValue1");
            modelState.AddModelError("TestKey2", "TestValue2");

            // Act
            var res = new ValidationFailedResult(modelState);
            var str = res.ToString();

            // Assert
            str.Should().Be("Validation Error (400): Model could not be validated. TestKey1 - TestValue1, TestKey2 - TestValue2");
        }