What is the best way to verify cookies are enabled for html 5 (mobile) website using asp.net mvc 3
I know that similar questions have been asked here but I am nevertheless having trouble getting it to work.
I am writing an html 5 website using ASP.NET MVC 3 that is targeted to mobile devices.
I had previously asked another question about how to store and retrieve expensive data and decided to go with Session variables.
I finished the app and started internal testing and all was well until we realized that the web site is not working on some iPhones. After researching I came to realize that Session variables depend on cookies and that some iPhones ship with cookies disabled by default.
Until I come up with a better solution, I want to check to see if cookies are enabled and redirect to an error page.
I was hoping that ALL of the code to do this could be written in a base controller but I could not figure out how to do it. So instead I put the following code into the base controller...
protected void WriteCookie()
{
Response.Cookies.Add(new HttpCookie("cookie_test"));
}
protected bool CookieCheck()
{
return (Request.Cookies["cookie_test"] != null);
}
Then in a home controller I am calling the WriteCookie method before loading the view. The view itself has javascript that is redirecting to one of two controllers / actions. The controller / view that will usually be called (because html 5 could not resolve the location) is the ChangeLocation / Index controller/action. In there I have the following code...
if (!CookieCheck())
{
RedirectToAction("CookieRequired", "Error");
}
Even with cookies disabled it is not redirecting to the above error page.
So what I am looking for is a best practices approach for solving this problem with a fairly complete code sample.
My home controller is pretty much ALWAYS redirecting to another page (in the javascript not the controller). I have a total of about 4 controllers with a total of about 6 actions.
My ideal would be to solve this problem completely in the base controller. If not that then I would love for it to work no matter what valid URL is typed (in other words, even if the web app is not entered through Home / Index). If not that then at least I want what I have shown in my code to work (set the cookie in Home / Index and check for it in the other controller / actions) and work even if the redirect is happening in a controller.