zoukankan      html  css  js  c++  java
  • C# MVC 进入Action 方法之后怎么使用MVC参数验证模型

    Action 接收的是json字符串,然后在把字符串序列化为实体,序列化完之后怎么再使用mvc自带的参数验证框架来验证参数, 这个问题困扰了我好久。也许有人说,调用接口的时候,加一个请求头,把contentype设置为application/json ,action 方法就可以用实体接受json的参数啦。但是我面对的不是这种情况。我需要解决的就是接受json字符串,然后序列化为实体。不要为我为什么,我就是那么固执。哈哈

    解决方案直接上代码,通过给Controller添加扩展方法满足了我的需求,有一点要注意的是,在Controller调用扩展方法时,一定要用this.xxx.好了上代码

    扩展方法:

    using System;
    using System.Collections;
    using System.Linq.Expressions;
    using System.Web.Mvc;
    
    namespace Lyt.Framework
    {
        public static partial class ControllerExtensions
        {
    
            public static bool ValidateModelExtensions(this Controller controller, object model) 
            {
                return ValidateModelExtensions(controller, model, null);
            }
    
            private static bool ValidateModelExtensions(this Controller controller, object model, string prefix = null) 
            {
                if (model == null)
                {
                    throw new ArgumentNullException("model");
                }
                if (model is IEnumerable)
                {
                    foreach (var item in model as IEnumerable)
                    {
                        ValidateModelBase(controller, item, prefix);
                    }
                }
                else
                {
    
                    ValidateModelBase(controller, model, prefix);
                }
                return controller.ModelState.IsValid;
            }
    
            private static void ValidateModelBase(Controller controller, object model, string prefix)
            {
                ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType());
    
                foreach (ModelValidationResult validationResult in ModelValidator.GetModelValidator(metadata, controller.ControllerContext).Validate(null))
                {
                    controller.ModelState.AddModelError(CreateSubPropertyName(prefix, validationResult.MemberName),validationResult.Message);
                }
            }
    
    
    
            private static string CreateSubPropertyName(string prefix, string propertyName)
            {
                if (String.IsNullOrEmpty(prefix))
                {
                    return propertyName;
                }
                else if (String.IsNullOrEmpty(propertyName))
                {
                    return prefix;
                }
                else
                {
                    return prefix + "." + propertyName;
                }
            }
        }
    }
    View Code

    调用示例:

    public ActionResult ValidateModelTest(string jsonModel)
            {
                List<PersonModel> list = JsonConvert.DeserializeObject<List<PersonModel>>(jsonModel);
                if (this.ValidateModelExtensions(list))
                {
                    return Content("ok");
                }
                else
                {
                    return Content("参数异常");
                }
            }
    View Code

    搞定。

    现在就可以用它去实现各种业务需求啦。

  • 相关阅读:
    Angular Universal 学习笔记
    SAP Spartacus 如何获得当前渲染页面的 CMS 元数据
    Angular 服务器端渲染的学习笔记(二)
    Angular 服务器端渲染的学习笔记(一)
    第三方外部 Saas提供商如何跟使用 SAP 系统的客户进行对接接口集成
    如何从 SAP Spartacus Product Detail 页面,找到其 Angular 实现 Component 的位置
    具备自动刷新功能的 SAP ABAP ALV 报表
    C++学习目录
    c--条件编译
    c--文件读写--二进制
  • 原文地址:https://www.cnblogs.com/liuyu7177/p/5581382.html
Copyright © 2011-2022 走看看