asp.net 后端验证
using EntryRegistration.Filters;
using EntryRegistration.Models.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace EntryRegistration.Models
{
public class Check
{
/// <summary>
/// 检查方法,支持泛型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instance"></param>
/// <returns></returns>
public static CheckResult CheckRequire<T>(T instance)
{ //初始化验证结果
CheckResult result = new CheckResult()
{
isSuccess = true,
msg = "验证OK"
};
//获取T类的属性
Type t = typeof(T);
var propertyInfos = t.GetProperties(); //遍历属性
foreach (var propertyInfo in propertyInfos)
{
//检查属性是否标记了特性
RequireAttribute attribute = (RequireAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(RequireAttribute)); //没标记,直接跳过
if (attribute == null)
{
continue;
} //获取该属性的值
var value = propertyInfo.GetValue(instance);
//获取属性的数据类型
if (typeof(string) == propertyInfo.PropertyType)
{
if (string.IsNullOrEmpty((string)value) && attribute.IsRequire)
{
result.isSuccess = false;
result.msg = propertyInfo.Name + "不能为空";
return result;
} }
else if (typeof(int?) == propertyInfo.PropertyType)
{
if (value == null && attribute.IsRequire)
{
result.isSuccess = false;
result.msg = propertyInfo.Name + "不能为空";
return result;
}
}
else if (typeof(Guid) == propertyInfo.PropertyType)
{
if (Guid.Parse(value.ToString()) == Guid.Empty && attribute.IsRequire)
{
result.isSuccess = false;
result.msg = propertyInfo.Name + "不能为空";
return result;
}
}
} return result;
}
}
}
//RequireAttribute.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace EntryRegistration.Filters
{
public class RequireAttribute : Attribute
{ public bool IsRequire { get; } /// <summary>
/// 构造函数
/// </summary>
/// <param name="isRequire"></param>
public RequireAttribute(bool IsRequire)
{
this.IsRequire = IsRequire;
}
}
}
asp.net 后端验证的更多相关文章
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证
原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...
- 【ASP.NET Core快速入门】(十五)MVC开发:ReturnUrl实现、Model后端验证 、Model前端验证
ReturnUrl实现 我们要实现returnUrl,我们需要在注册(Register)方法中接收传进的returnUrl并给它默认值null,然后将它保存在ViewData里面 然后我们定义一个内部 ...
- 菜鸟入门【ASP.NET Core】15:MVC开发:ReturnUrl实现、Model后端验证 、Model前端验证
ReturnUrl实现 我们要实现returnUrl,我们需要在注册(Register)方法中接收传进的returnUrl并给它默认值null,然后将它保存在ViewData里面 然后我们定义一个内部 ...
- Asp.net MVC验证那些事(1)-- 介绍和验证规则使用----[转]--[并修改了部分内容]
Asp.net MVC验证那些事(1)-- 介绍和验证规则使用 -----原文地址链接 数据的有效性验证,是程序开发中必不可少的环节.这篇文章,我们将用一个实例来说明如何在MVC中使用Validati ...
- MyCat源码分析系列之——前后端验证
更多MyCat源码分析,请戳MyCat源码分析系列 MyCat前端验证 MyCat的前端验证指的是应用连接MyCat时进行的用户验证过程,如使用MySQL客户端时,$ mysql -uroot -pr ...
- Asp.net MVC验证哪些事(2)-- 验证规则总结以及使用
上篇文章Asp.net MVC验证那些事(1)-- 介绍和验证规则使用中,介绍了Asp.net MVC中的验证功能以及如何使用.这里将对MVC中内置的验证规则进行总结. 一,查找所有验证规则 上篇文章 ...
- Asp.net MVC验证那些事(4)-- 自定义验证特性
在项目的实际使用中,MVC默认提供的Validation Attribute往往不够用,难以应付现实中复杂多变的验证需求.比如, 在注册用户的过程中,往往需要用户勾选”免责声明”,这个checkbox ...
- 【ASP.NET】编程点滴 :ASP.NET身份验证
ASP.NET实际开发中身份验证 是一个不可回避的问题.在相当一段长的时间内,由于不求甚解,我对这个话题似懂非懂.今天就对它做个简单的小结. Authentication and Authorizat ...
- asp.net检查验证字符串是否为纯数字方法小结
原文 asp.net检查验证字符串是否为纯数字方法小结 在asp.net中验证字符串是不是为数字我们没有像php中那么多丰富的函数来直接使用,这里我整理了一些比较实例的验证字符串是否为纯数字方法代码 ...
随机推荐
- Angularjs Module类的介绍及模块化依赖
后面的学习我们会遵循一个控制器管理一个视图,一个路由对应一个视图的单一原则,所以再不会将controller控制器代码直接写到 index.html 中. 我们会应用到angular.js中强大的模块 ...
- git使用(一)----git安装
windows安装git msysgit是windows版本的Git 下载地址:https://git-for-windows.github.io/ 安装步骤 linux安装git https://g ...
- IP首部
1. 引言 IP是TCP/IP协议族中最为核心的协议.所有的TCP.UDP.ICMP及IGMP数据都以IP数据报格式传输,但是IP提供不可靠.无连接的数据报传送服务.不可靠的意思是它不能保证IP数据报 ...
- [sql]MySQL数据备份小结
一 MySQL备份恢复总结: 1,备份所有库 2,分库备份 3,备份某库中的某表 4,备份某库中的多个表 5,分表备份 6,只备份表结构 7,只备份数据 二 MySQL备份恢复参数总结: -A 备份所 ...
- android笔记-----消息提示
在/res/values目录下的文件中定义要显示的字符串,主要是考虑到后期可能需要换成英文之类的 <string name="login_checkBlank">用户名 ...
- angular -- $route API翻译
$route -$routeProvider服务 -依赖ngRoute模块 $route能够在路径发生改变的时候,渲染不同的视图,调用不同的控制器.它监测了$location.url(),然后根据路径 ...
- 每日英语:Is It Possible To Reason About Having A Child?
How can you decide whether to have a child? It's a complex and profound question -- a philosophical ...
- linux命令(28):Linux下SCP无需输入密码传输文件,python 中scp文件
python 中scp文件:(如果下面的发送免密码已经完成的话,就直接能用下面这个) os.system('scp "%s" "%s:%s"' % (" ...
- jquery 时间戳和日期时间转化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 接Window服务(二)
接Window服务(一) ServiceController方法调用 1 public partial class Service1 : ServiceBase 2 { 3 public Servic ...