.NetCore 在不同位置添加过滤器
2024-09-05 05:45:20
前言
以ParaModelValidateAttribute(参数校验)和ErrorCatch(错误捕捉)为例。
在方法上添加(局部)
这种方式比较灵活
[ParaModelValidate]
[ErrorCatch]
public class BaseController : ControllerBase
{
public ContentResult JsonResult(dynamic data = null)
{
ContentResult result = JsonContentResultBuilder.BuildViewJsonResult(data);
return result;
}
}
在Startup中添加(全局)
这种方式应用于所有方法
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddControllersWithViews().AddNewtonsoftJson();
#region 配置过滤器
services.AddControllersWithViews(options => {
options.Filters.Add(typeof(ParaModelValidateAttribute));
});
services.AddControllersWithViews(options => {
options.Filters.Add(typeof(ErrorCatchAttribute));
});
#endregion
}
最新文章
- Trace Flag
- ssh的原理和流程
- android 调用地图
- HttpClient请求返回JSON、图片
- [JS3] 立即执行JS
- Code First Migrations更新数据库结构的具体步骤
- 【oracle】数据库、表空间、用户、数据表之间的关系
- 利用改进的cca算法,进行识别
- POJ - 1422 Air Raid 二分图最大匹配
- NeuChar 平台使用及开发教程(三):使用 NeuChar 的菜单服务
- Python全栈开发记录_第四篇(集合、函数等知识点)
- mongodb的db.collection is not function
- c# 字典
- 738. Monotone Increasing Digits 单调递增的最接近数字
- 理解Java枚举类型
- case关联表查询
- 监控Tomcat
- 发布上线前,先小秀一把俺的64位浏览器,速度那觉对是杠杠滴,上youtube,上google不费劲
- 11 python shutil 模块
- Linux内核中断处理机制
热门文章
- iOS 自定义tabBarController(中间弧形)
- Flutter ListTile - Flutter每周一组件
- C# 生成6位短信验证码
- Nginx 配置日志路径(nginx.conf没有写log路径,所以debug的时候找不到日志)
- 由两个问题引发的对GaussDB(DWS)负载均衡的思考
- Linux腾讯云下安装mysql
- 3. Longest Substring Without Repeating Characters寻找不重复的最大子串
- 基于Opencv的简单图像处理
- 栈面板类StackPane?
- Java学习日报7.19