Search code examples
asp.net-mvctempdata

ASP.NET MVC Store TempData in Cookie


Is there a way to let TempData store in a browser's cookie instead of Session State. I have Session State disabled on my site.

Thanks.


Solution

  • Nazaf,

    try this for removing your cookies:

    public void DeleteCookie(string name)
    {
        DateTime now = DateTime.UtcNow;
        string cookieKey = name;
        var cookie = new HttpCookie(cookieKey, null)
        {
            Expires = now.AddDays(-1)
        };
        HttpContext.Response.Cookies.Set(cookie);
    }
    

    usage:

    DeleteCookie("__ControllerTempData");