I'm using this code to determine if my page is refreshed but it's always coming back as true
public class RefreshDetectFilter : ActionFilterAttribute, IActionFilter
{
//void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var cookie = filterContext.HttpContext.Request.Cookies["RefreshFilter"];
filterContext.RouteData.Values["IsRefreshed"] = cookie != null &&
cookie.Value == filterContext.HttpContext.Request.Url.ToString();
}
//void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.SetCookie(new HttpCookie("RefreshFilter", filterContext.HttpContext.Request.Url.ToString()));
}
}
in my controller I have the attribute and the check
[RefreshDetectFilter]
public class UserController : Controller
in the action method
if (Convert.ToBoolean(RouteData.Values["IsRefreshed"]) == true)
{
// page has been refreshed.
}
I suspect you are not setting the cookie early enough in the request for it to have any effect. Per MSDN:
Although, if you look at the MVC life cycle overview, the action filter events don't really line up with ASP.NET page events so it is difficult to tell without some experimentation. That said, I suspect you need to do: