最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)防止重復(fù)提交的代碼詳解

 更新時間:2025年09月12日 09:20:21   作者:李袁明  
當(dāng)用戶在前端進行提交數(shù)據(jù)時,如果網(wǎng)絡(luò)出現(xiàn)卡頓和前端沒有給出響應(yīng)的話顧客通常都會狂點提交按鈕,這樣就很容易導(dǎo)致后端數(shù)據(jù)造成臟數(shù)據(jù),所以本文給大家介紹了如何避免C#重復(fù)提交,需要的朋友可以參考下

前言

當(dāng)用戶在前端進行提交數(shù)據(jù)時,如果網(wǎng)絡(luò)出現(xiàn)卡頓和前端沒有給出響應(yīng)的話顧客通常都會狂點提交按鈕,這樣就很容易導(dǎo)致后端數(shù)據(jù)造成臟數(shù)據(jù),眾所周知顧客就是老天爺,這種問題一定是咱有問題。下面我們就來看看如何避免這種情況吧。

防止重復(fù)提交的思路

防止重復(fù)提交我們可以才用緩存的方式存儲一個key在我們的Redis或者其他類型的緩存,再給它設(shè)置一個過期時間比如五秒或者三四秒,這個時間最好不要設(shè)置的過長,不然會影響用戶體驗。值得注意的是這個key一定要是與用戶一一對應(yīng)且不會重復(fù)的!

Web API 防止重復(fù)提交

代碼實現(xiàn)

以下是具體的代碼實現(xiàn):

// <summary>
	/// 防重復(fù)提交,api使用
	/// </summary>
	public class LockAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 攔截
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            if (GlobalContext.SystemConfig.Debug == false)
            {
                if (OperatorProvider.Provider.GetCurrent() == null)
                {
                    context.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "抱歉,沒有操作權(quán)限" });
                    return;
                }
                else
                {
                    string token = context.HttpContext.Request.Headers[GlobalContext.SystemConfig.TokenName].ParseToString();
                    if (string.IsNullOrWhiteSpace(token))
                    {
                        context.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "token不能空" });
                        return;
                    }
                    //固定加鎖5秒
                    bool result = CacheHelper.SetNx(token, token, 5);
                    if (!result)
                    {
                        context.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "請求太頻繁,請稍后" });
                        return;
                    }
                }
            }
            await next();

            sw.Stop();
        }
    }

代碼講解

  • GlobalContext.SystemConfig.Debug :這里是讀取配置文件判斷是否為開發(fā)環(huán)境
  • OperatorProvider.Provider.GetCurrent():獲取顧客在系統(tǒng)中的權(quán)限
  • context.HttpContext.Request.Headers[GlobalContext.SystemConfig.TokenName]:這里是通過上線文獲取顧客的Token
  • CacheHelper.SetNx:這里是設(shè)置緩存:該方法會查詢緩存中是否已有該用戶的緩存信息,如果已經(jīng)有了則返回false,沒有就設(shè)置該用戶的緩存并返回true。

使用方法

直接以特性的形似使用,在需要進行鎖定的接口上標(biāo)注即可

MVC防止重復(fù)提交

public class HandlerLockAttribute : ActionFilterAttribute
{
	public HandlerLockAttribute()
	{
	}

	public override void OnActionExecuting(ActionExecutingContext filterContext)
	{
		if (OperatorProvider.Provider.GetCurrent() == null)
		{
			WebHelper.WriteCookie("WaterCloud_login_error", "overdue");
			//filterContext.HttpContext.Response.WriteAsync("<script>top.location.href ='" + filterContext.HttpContext.Request.PathBase + "/Home/Error?msg=408" + "';if(document.all) window.event.returnValue = false;</script>");
			OperatorProvider.Provider.EmptyCurrent("pc_").GetAwaiter().GetResult();
			filterContext.Result = new RedirectResult(filterContext.HttpContext.Request.PathBase + "/Home/Error?msg=408");
			return;
		}
		else
		{
			string token = filterContext.HttpContext.Request.Cookies["pc_" + GlobalContext.SystemConfig.TokenName];
			string cacheToken = CacheHelper.GetAsync<string>("pc_" + GlobalContext.SystemConfig.TokenName + "_" + OperatorProvider.Provider.GetCurrent().UserId + "_" + OperatorProvider.Provider.GetCurrent().LoginTime).GetAwaiter().GetResult();
			if (string.IsNullOrWhiteSpace(token))
			{
				filterContext.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "token不能空" });
				return;
			}
			if (string.IsNullOrWhiteSpace(cacheToken))
			{
				filterContext.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "token不能空" });
				return;
			}
			if (token != cacheToken)
			{
				filterContext.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "請求異常" });
				return;
			}
			//固定加鎖5秒
			bool result = CacheHelper.SetNx(token, token, 5);
			if (!result)
			{
				filterContext.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "請求太頻繁,請稍后" });
				return;
			}
		}
		//隨機值
		base.OnActionExecuting(filterContext);
	}
}
  • OperatorProvider.Provider.EmptyCurrent(“pc_”).GetAwaiter().GetResult():此處是判斷如果顧客信息為空的話就清空當(dāng)前登錄賬戶的緩存
  • CacheHelper.SetNx:這里是設(shè)置緩存:該方法會查詢緩存中是否已有該用戶的緩存信息,如果已經(jīng)有了則返回false,沒有就設(shè)置該用戶的緩存并返回true。

總結(jié)

到此這篇關(guān)于C#實現(xiàn)防止重復(fù)提交的代碼詳解的文章就介紹到這了,更多相關(guān)C#防止重復(fù)提交內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

宜兰市| 阜康市| 乐业县| 思茅市| 石河子市| 曲水县| 嘉峪关市| 栖霞市| 临湘市| 西青区| 万宁市| 龙岩市| 虎林市| 甘泉县| 海林市| 东乡族自治县| 郴州市| 商城县| 神农架林区| 上犹县| 达尔| 和政县| 友谊县| 黄大仙区| 神农架林区| 伊川县| 北流市| 福州市| 广元市| 辽宁省| 饶阳县| 黑龙江省| 永城市| 布拖县| 宜昌市| 腾冲县| 临湘市| 册亨县| 井陉县| 安泽县| 阿坝县|