我在asp.net mvc
是新的,我需要檢測視圖頁面刷新與用戶做一些事情爲目的的閱讀:
asp.net mvc - Detecting page refresh
用於定義動作過濾器上的文件夾控制器點擊右鍵並添加RefreshDetectFilter
類:
爲什麼我的操作過濾器不起作用?
namespace StoreProject.Controllers
{
public class RefreshDetectFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var cookie = filterContext.HttpContext.Request.Cookies["RefreshFilter"];
filterContext.RouteData.Values["IsRefreshed"] = cookie != null &&
cookie.Value == filterContext.HttpContext.Request.Url.ToString();
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.SetCookie(new HttpCookie("RefreshFilter", filterContext.HttpContext.Request.Url.ToString()));
}
}
}
並註冊,在Global.asax
,用這種方式:
GlobalFilters.Filters.Add(new RefreshDetectFilter());
我的行動
要使用行爲過濾器使用此代碼:
if (RouteData.Values["IsRefreshed"] == true)
{
// page has been refreshed.
}
投的數據則比較'如果((布爾)RouteData.Values [「IsRefreshed」] )' – Nkosi