asp.net 远程模型验证

有这样一些场景,我们需要模型验证,某些字段不允许重复,但是又不希望在数据访问层增加一堆额外逻辑判断。
我们需要数据访问层简洁,这种模型验证在进去Action之前,验证不通过直接告诉前端。

一个特性,继承ValidationAttribute,实现
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
重载

代码段附上

public class RemoteValidateAttribute: ValidationAttribute
    {
        public string Controller { get; set; }
        public string Action { get; set; }
        public string HttpMethod { get; set; } = "GET";

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var url = $"{HostInformat.Instance.Host}/{Controller}/{Action}";
            var client = new RestClient(url);
            var request = new RestRequest(HttpMethod.Equals("POST", StringComparison.InvariantCultureIgnoreCase) ? Method.POST : Method.GET);

            request.AddParameter(validationContext.MemberName, value);

            var response = client.Execute<ValidateInformat>(request);

            if (response.IsSuccessful)
            {
                var result = response.Data;

                if (result.Status)
                {
                    return ValidationResult.Success;
                }

                return new ValidationResult(result.Message);
            }

            return new ValidationResult($"Action:{url}无法访问");
        }
    }
    public class ValidateInformat
    {
        public bool Status { get; set; }
        public string Message { get; set; }

        public static implicit operator ValidateInformat(string message)
        {
            return new ValidateInformat()
            {
                Status = false,
                Message = message
            };
        }

        public static implicit operator ValidateInformat(bool status)
        {
            return new ValidateInformat()
            {
                Status = status,
                Message = null
            };
        }
    }
    public class HostInformat
    {
        public HostInformat(IConfiguration conf)
        {
            Host = conf["urls"];

            if (Instance == null)
            {
                Instance = this;
            }
        }

        public string Host { get; }

        public static HostInformat Instance { get; set; }
    }

调用

[RemoteValidate(Action = "FindPlatformUserForAccount", Controller = "Validata", HttpMethod = "Post")]
public string Account { get; set; }

验证的Action

    public class ValidataController : Controller
    {
        [HttpPost]
        public ValidateInformat FindPlatformUserForAccount(string Account)
        {
            var result = PlatformUserHelper.ExistsForAccount(Account);

            if (result)
            {
                return "账号名已存在";
            }

            return true;
        }
    }

网络请求部分是用的RestSharp

在Startup.cs
ConfigureServices方法内增加如下代码

            services.AddSingleton<IConfiguration>(Configuration);
            services.AddSingleton<HostInformat>();

Configure方法内增加如下代码

            var hostInformat = app.ApplicationServices.GetRequiredService<HostInformat>();
            HostInformat.Instance = hostInformat;
posted @ 2018-10-15 14:52  沉迷代码的萌新  阅读(167)  评论(1编辑  收藏  举报