asp.net mvc 防止重复提交

string hiddenToken = "hiddenToken";
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string httpMethod = filterContext.RequestContext.HttpContext.Server.HtmlEncode(filterContext.RequestContext.HttpContext.Request.HttpMethod);
 
            if (httpMethod == "POST")
            {
                // page token
                // hiddenToken
                string cacheToken = filterContext.HttpContext.Request[hiddenToken];
                //filterContext.HttpContext.Request.IsAjaxRequest()
                if (cacheToken != null)
                {
                    if (System.Web.HttpContext.Current.Cache[cacheToken] == null)
                    {
                        System.Web.HttpContext.Current.Cache.Insert(cacheToken, cacheToken, null, DateTime.MaxValue, TimeSpan.FromSeconds(1));
                        LogHeper.Instant.log.Info("提交成功!");
                    }
                    else
                    {
                        LogHeper.Instant.log.Info(filterContext.HttpContext.Request.Url + "请不要重复提交!");
                        throw (new GLRISCRMException("请不要重复提交!"));
                    }
                }
            }
        }

 

@Html.Hidden("hiddenToken", new Guid().ToString() + DateTime.Now.Ticks)

 

posted @ 2016-05-26 17:09  Red Cat  阅读(3449)  评论(2编辑  收藏  举报

Copyright © 2022 LyShark Powered by .NET 6 on Kubernetes
Theme - LyTheme 1.0