.net core mvc model填充过滤器
在程序开发中,我们可能经常遇到所有的数据库表有相同的属性和行为,比如需要记录数据的创建人员,创建时间,修改时间和修改人。如果在每个action中都加上这些信息,代码看着比较冗余,看着不那么优雅,于是考虑添加一个过滤器,在请求进入aciton之前对模型进行填充。这样我们就不必要在每个action中进行创建时间或者登录人员的信息进行复制一类的操作,使编程过程更加专注于业务。同时,我们也可以在这里进行一些关键词的过滤或者英文单引号过滤。
在这里我选择使用的是ActionFilterAttribute,通过重写OnActionExecuting方法填充model。具体实现代码如下:
public class ModelFillFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context); var parameters = context.ActionArguments; parameters.ForEach(parameter =>
{
var model = parameter.Value;
if (model == null) return;
var list = new ArrayList(); if (typeof(ICollection).IsAssignableFrom(model.GetType()))
{
list.AddRange(model as ICollection);
}
else
{
list.Add(model);
} list.ToArray().ForEach(item =>
{
var propertys = item.GetType().GetProperties();
propertys.ForEach(p =>
{
// 替换' 解决sql注入问题
if (p.PropertyType.Name.ToLower().Contains("string") && p.GetValue(item) != null && p.GetSetMethod() != null)
{
p.SetValue(item, p.GetValue(item).ToString().Replace("'", "''"));
}
});
});
}); var tokenObj = context.HttpContext.Request.Form["token"]; if (!string.IsNullOrEmpty(tokenObj))
{
var token = tokenObj.ToString();
var userInfoService = Ioc.GetService<IUserInfoService>();
var user = userInfoService.Get<UserInfoModel>(string.Format(" LoginToken='{0}'", token)); if (user != null)
{
parameters.ForEach(parameter =>
{
var model = parameter.Value;
if (model == null) return; var list = new ArrayList(); if (typeof(ICollection).IsAssignableFrom(model.GetType()))
{
list.AddRange(model as ICollection);
}
else
{
list.Add(model);
} list.ToArray().ForEach(item =>
{
var propertys = item.GetType().GetProperties();
//模型处于创建状态
bool isCreate = propertys.Any(p => p.Name.ToLower() == "id" &&
(p.GetValue(item) == null ||
string.IsNullOrEmpty(p.GetValue(item).ToString()) ||
p.GetValue(item).ToString() == ""));
if (isCreate)
{
propertys.ForEach(p =>
{
//字段填充
if (p.Name.ToLower() == "createdby" && p.GetSetMethod() != null && user != null)
p.SetValue(item, Convert.ToInt32(user.Id));
else if (p.Name.ToLower() == "createdat" && p.GetSetMethod() != null)
p.SetValue(item, DateTime.Now);
});
} //模型处于编辑状态
bool isUpdate = propertys.Any(p => p.Name.ToLower() == "id" &&
(p.GetValue(item) != null &&
!string.IsNullOrEmpty(p.GetValue(item).ToString()) &&
p.GetValue(item).ToString() != ""));
if (isUpdate)
{
propertys.ForEach(p =>
{
//字段填充
if (p.Name.ToLower() == "updatedby" && p.GetSetMethod() != null && user != null)
p.SetValue(item, Convert.ToInt32(user.Id));
else if (p.Name.ToLower() == "updatedat" && p.GetSetMethod() != null)
p.SetValue(item, DateTime.Now);
});
} //既不是创建也不是编辑状态
if (!isCreate && !isUpdate)
{
propertys.ForEach(p =>
{ });
}
});
});
}
}
} /// <summary>
/// 清楚敏感词汇
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private bool IsContainKey(string key)
{
return false;
}
}
在代码实现过程中,这里是通过判断id是否有值用来判断这次请求是添加还是修改操作,以便进行不同的赋值。都可以根据自身情况进行不同的判断。
.net core mvc model填充过滤器的更多相关文章
- .net core MVC 通过 Filters 过滤器拦截请求及响应内容
前提: 需要nuget Microsoft.Extensions.Logging.Log4Net.AspNetCore 2.2.6: Swashbuckle.AspNetCore 我暂时用的是 ...
- .net core MVC Filters 过滤器介绍
一.过滤器的优级依次介绍如下(逐次递减): Authorization Filter -> Resource Filter -> Acton Filter -> Exception ...
- ASP.NET Core MVC 过滤器介绍
过滤器的作用是在 Action 方法执行前或执行后做一些加工处理.使用过滤器可以避免Action方法的重复代码,例如,您可以使用异常过滤器合并异常处理的代码. 过滤器如何工作? 过滤器在 MVC Ac ...
- asp.net core MVC 过滤器之ActionFilter过滤器(二)
本系类将会讲解asp.net core MVC中的内置全局过滤器的使用,将分为以下章节 asp.net core MVC 过滤器之ExceptionFilter过滤器(一) asp.net core ...
- .Net Core MVC 过滤器(一)
1.过滤器 过滤器运行在MVC Action Invocation Pipeline(MVC Action 请求管道),我们称它为Filter Pipleline(过滤器管道),Filter Pi ...
- 008.Adding a model to an ASP.NET Core MVC app --【在 asp.net core mvc 中添加一个model (模型)】
Adding a model to an ASP.NET Core MVC app在 asp.net core mvc 中添加一个model (模型)2017-3-30 8 分钟阅读时长 本文内容1. ...
- asp.net core MVC 全局过滤器之ExceptionFilter异常过滤器(一)
本系类将会讲解asp.net core MVC中的内置全局过滤器的使用,将分为以下章节 asp.net core MVC 过滤器之ExceptionFilter异常过滤器(一) asp.net cor ...
- 解说asp.net core MVC 过滤器的执行顺序
asp.net core MVC 过滤器会在请求管道的各个阶段触发.同一阶段又可以注册多个范围的过滤器,例如Global范围,controller范围等.以ActionFilter为例,我们来看看过滤 ...
- Asp.Net Core MVC框架内置过滤器
第一部分.MVC框架内置过滤器 下图展示了Asp.Net Core MVC框架默认实现的过滤器的执行顺序: Authorization Filters:身份验证过滤器,处在整个过滤器通道的最顶层.对应 ...
随机推荐
- vue,一路走来(2)--路由vue-router
安装 Mint UI cnpm install mint-ui --save 如果你的项目会用到 Mint UI 里较多的组件,最简单的方法就是把它们全部引入.此时需要在入口文件 main.js 中: ...
- 数据库_MHA群集搭建
MHA概念介绍,群集搭建与测试 一, MHA介绍 1.概念:MHA master high availability,由日本DeNA公司开发,解决mysql故障切换可以做到0-30秒,而且在故障切换过 ...
- weblogic启动脚本
DATE=`date +%Y%m%d%H%M%S` user=`whoami` logDir=/app/logs/sguap_admin #启动日志存放路径sguap是例子系统简称# logDestd ...
- 在IDEA中如何将Spring boot项目打包成可执行的jar包并发布到linux服务
这两年微服务很流行,这里简单介绍一下如何将自己使用idea写的微服务打包成一个可执行的jar包,并发布到linux服务器的步骤.因为spring boot有内置的tomcat所以一般使用内置的tomc ...
- html5 固定边栏滚动特效
<script src="https://code.jquery.com/jquery.js"></script> //引入jquery <scrip ...
- LINUX Mysql5.6.19 安装
1.需要扩展安装 yum -y install make bison gcc-c++ cmake ncurses ncurses-devel 2.下载Mysql5.6.19 wget ftp://mi ...
- 跨域共享cookie
1. JSP中Cookie的读写 Cookie的本质是一个键值对,当浏览器访问web服务器的时候写入在客户端机器上,里面记录一些信息.Cookie还有一些附加信息,比如域名.有效时间.注释等等. 下面 ...
- Apache搭建http网站服务器入门教程
Apache搭建http网站服务器入门教程 准备工具 一台带有Linux系统的主机,这里使用CentOS 7.1 64位系统 一个备案过的域名,这里使用www.hellopage.cn 一台可以访问网 ...
- LUOGU P3380 【模板】二逼平衡树(树套树)
传送门 解题思路 这里写的是常数巨大的线段树套\(splay\),卡了半天常才过.首先线段树每个节点挂一个\(splay\),\(splay\)中的元素即为线段树管辖的区间中的数.对于操作\(1\), ...
- Spring CGLlB动态代理
JDK 动态代理使用起来非常简单,但是它也有一定的局限性,这是因为 JDK 动态代理必须要实现一个或多个接口,如果不希望实现接口,则可以使用 CGLIB 代理. CGLIB(Code Generati ...