How can I centralize modelstate validation in asp.net mvc using action filters?

后端 未结 4 1820
一生所求
一生所求 2020-12-05 01:11

I write this code in several places and always repeat this logic:

public ActionResult MyMethod(MyModel collection)
{
    if (!ModelState.IsValid)
    {
              


        
相关标签:
4条回答
  • 2020-12-05 01:13

    To conform with REST, you should return http bad request 400 to indicate that the request is malformed (model is invalid) instead of returning Json(false).

    Try this attribute from asp.net official site for web api:

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

    A version for asp.net mvc could be like this:

    public class ValidateModelAttribute : ActionFilterAttribute
    {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                  if (!filterContext.Controller.ViewData.ModelState.IsValid)
                  {
                       filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
                  }
            }
    }
    
    0 讨论(0)
  • 2020-12-05 01:16
    public class ValidateModelStateAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {              
                context.Result = new ViewResult();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 01:28

    If you want to do this in MVC6 or Mvc Core and without specifying your attribute on all of your Action methods then this is how you do it.

    First create your ActionFilter

    public class ModelStateValidationFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting( ActionExecutingContext context )
        {
            if ( context.HttpContext.Request.Method == "POST" && !context.ModelState.IsValid )
                context.Result = new BadRequestObjectResult( context.ModelState );
        }
    }
    

    Now create a convention in which you will apply this ActionFilter to all of your controllers.

        public class ModelStateValidatorConvension : IApplicationModelConvention
        {
            public void Apply( ApplicationModel application )
            {
                foreach ( var controllerModel in application.Controllers )
                {
                    controllerModel.Filters.Add( new ModelStateValidationFilterAttribute() );
                }
            }
        }
    

    And the last thing is to register this convention in MVC

        public void ConfigureServices( IServiceCollection services )
        {
          services.Configure<MvcOptions>( x => x.Conventions.Add( new ModelStateValidatorConvension() ) );
        }
    
    0 讨论(0)
  • 2020-12-05 01:31

    Here is how to use the code from Khanh TO (from asp.net official site):

    To apply this filter to all Web API controllers, add an instance of the filter to the HttpConfiguration.Filters collection during configuration:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Filters.Add(new ValidateModelAttribute());
    
            // ...
        }
    }
    

    Another option is to set the filter as an attribute on individual controllers or controller actions:

    public class ProductsController : ApiController
    {
      [ValidateModel]
      public HttpResponseMessage Post(Product product)
      {
        // ...
      }
    }
    
    0 讨论(0)
提交回复
热议问题