FluentValidation does not work when using the WebApi [Route] attribute

I successfully implemented FluentValidation in my WebApi project controller, which had only one HttpGet method. When I added another HttpGet method, I added a route attribute to both methods. those. [Route ("Method1")] and [Route ("Method2")].

ModelState now returns as true, regardless of whether I enter any data or not.

Here is my code.

WebApiConfig

public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Filters.Add(new ValidateModelStateFilter()); //FluentValidation FluentValidationModelValidatorProvider.Configure(config); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{action}/{id}", defaults: new { controller = "Menu", id = RouteParameter.Optional} ); } } 

ValidateModelStateFilter

 public class ValidateModelStateFilter : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (!actionContext.ModelState.IsValid) { actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState); } } } 

controller

 [HttpGet] [Route("Method1")] public IHttpActionResult ReadAllMenusByApplication([FromUri] ReadAllMenusByApplicationInput input) { var result = new List<ApplicationMenu>(); ... } 

Input object

 using FluentValidation; using FluentValidation.Attributes; namespace MenuService.Models { [Validator(typeof(ReadAllMenusByApplicationInputValidator))] public class ReadAllMenusByApplicationInput { public ReadAllMenusByApplicationInput() { this.ApplicationName = string.Empty; } /// <summary> /// The MenuSystem name of the application /// </summary> public string ApplicationName { get; set; } } public class ReadAllMenusByApplicationInputValidator : AbstractValidator<ReadAllMenusByApplicationInput> { public ReadAllMenusByApplicationInputValidator() { RuleFor(x => x.ApplicationName).NotEmpty(); } } 

}

+7
c # asp.net-web-api fluentvalidation model-validation
source share
1 answer

Use this article for reference.

Custom Validation in ASP.NET Web API with FluentValidation

You seem to have done most of what has been done in this article.

Check your configuration order.

 public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services config.Filters.Add(new ValidateModelStateFilter()); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{action}/{id}", defaults: new { controller = "Menu", id = RouteParameter.Optional} ); //FluentValidation FluentValidationModelValidatorProvider.Configure(config); } } 

FluentValidation automatically inserts its errors into ModelState . You must include an error message.

 public class ReadAllMenusByApplicationInputValidator : AbstractValidator<ReadAllMenusByApplicationInput> { public ReadAllMenusByApplicationInputValidator() { RuleFor(x => x.ApplicationName).NotEmpty() .WithMessage("The Application Name cannot be blank."); } } 

The article has content that goes beyond your question. basically wrapping answers, but everything else should work for you.

0
source share

All Articles