posts - 303,  comments - 59,  views - 44万
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

https://izen.live/Blog/info/13.html

/// <summary>
/// action方法过滤器
/// </summary>
public class PlatformActionFilter : Attribute, IActionFilter
{
    private static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
    public const string hiddenToken = "hiddenToken";
    private ILog _log;

    public PlatformActionFilter()
    {
        this._log = LogManager.GetLogger(Startup.Repository.Name, typeof(PlatformActionFilter));
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {

    }
    /// <summary>
    /// action 执行之前
    /// </summary>
    /// <param name="context"></param>
    public virtual void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string httpMethod = WebUtility.HtmlEncode(filterContext.HttpContext.Request.Method);
        if (httpMethod == "POST")
        {
            //使用请求路径作为唯一key
            string path = filterContext.HttpContext.Request.Path;
            string cacheToken = $"{hiddenToken}_{path}";
            string keyValue = new Guid().ToString() + DateTime.Now.Ticks;

            if (path != null)
            {
                //var cache = iZen.Utils.Core.iCache.CacheManager.GetCacheValue(cacheToken);
                var cv = cache.Get(cacheToken);
                if (cv == null)
                {
                    //iZen.Utils.Core.iCache.CacheManager.SetChacheValueSeconds(cacheToken, keyValue, 1);
                    //设置缓存1秒过期
                    cache.Set(cacheToken, keyValue, new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromSeconds(1) });
                    _log.Info($"提交成功");
                }
                else
                {
                    _log.Error($"{filterContext.HttpContext.Request.Method},请不要重复提交");
                    //设置了 filterContext.Result 表示返回过滤失败的结果
                    //filterContext.Result = new BadRequestObjectResult(filterContext.ModelState);
                    filterContext.Result = new BadRequestObjectResult("请不要重复提交");
                }

            }
            return;
        }
        this.OnActionExecuting(filterContext);
    }

}

action上添加过滤器特性

/// <summary>
/// 测试重复提交过滤器
/// </summary>
/// <returns></returns>
[PlatformActionFilter]
[HttpPost]
public JsonResult TestPost()
{
    var result = new ResultModel() { IsSuccess = true, Info = "测试重复提交" };
    return Json(result);
}
posted on   芝麻的西瓜  阅读(433)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2018-03-07 jquery关于attr和prop的差异
2014-03-07 Unity3d 物体沿着正七边形轨迹移动
2014-03-07 abstract class 和 interface区别
2014-03-07 ref和out
2014-03-07 .Net配置错误页
2014-03-07 .net简单的静态页生成
2014-03-07 url重写步骤
点击右上角即可分享
微信分享提示