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中那么多丰富的函数来直接使用,这里我整理了一些比较实例的验证字符串是否为纯数字方法代码 ...
随机推荐
- C# xml可序列化多值枚举脚本
代码: using System; using System.Collections.Generic; using System.Xml; using System.Xml.Schema; using ...
- 【Android】7.5 RelativeLayout(相对布局)
分类:C#.Android.VS2015: 创建日期:2016-02-11 一.简介 RelativeLayout是一种相对布局,容器中子元素的位置是相对于其前一个元素或者其他元素的位置来计算的,或者 ...
- Effective JavaScript Item 46 优先使用数组而不是Object类型来表示有顺序的集合
本系列作为Effective JavaScript的读书笔记. ECMAScript标准并没有规定对JavaScript的Object类型中的属性的存储顺序. 可是在使用for..in循环对Objec ...
- iPython网页启动
安装必要的软件包: pip install "ipython[all]" 启动命令:ipython notebook --inline=pylib 自动采用默认浏览器打开 ht ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- nginx正向代理http(一)
nginx实现正向代理,下面以http为例说明: (1)nginx配置: server { listen 8080; resolver 114.114.114.114; access_log logs ...
- Frosh Week
Problem Description During Frosh Week, students play various fun games to get to know each other and ...
- Activiti - 新一代的开源BPM引擎
Activiti 背景简介.服务和功能介绍 背景介绍 Activiti 其核心是 BPMN 2.0 的流程引擎.BPMN 是目前被各 BPM 厂商广泛接受的 BPM 标准,全称为 Business P ...
- ADO中记录集recordSet的使用
_RecordsetPtr使用方法 _variant_t vUsername,vID,vname; //变量声明_RecordsetPtr m_pRecordset; //记录集CString ...
- 基于CSS3和jQuery实现的3D相册
天我们再来看一款HTML5 3D相册浏览应用,图片可以手动播放,也可以自动播放,效果非常震撼,赶紧把这款HTML5 3D相册分享给你的朋友吧. 在线预览 源码下载 实现的代码: <div c ...