如何在mvc4中创建多个cookie? [英] How to create multiple cookies in mvc4?

查看:106
本文介绍了如何在mvc4中创建多个cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果已经为同一个应用程序创建了cookie,如何创建新的cookie?



场景:我已经获取数据并存储到会话中,然后cookie已创建会话ID。

第二次我想在另一个cookie中维护不同的数据是否可以在mvc4中?

How to create new cookie if the cookie is already create for same application?

Scenario: I have fetched the data and stored into session then cookie has created with session id.
second time i want to maintain different data in another cookie is it possible in mvc4?

推荐答案

请遵循此代码.. .......

Follow this code.........
public class CookieController : Controller
    {

        public ActionResult Create()
        {
            HttpCookie cookie = new HttpCookie("Cookie");
            cookie.Value = "Hello Cookie! CreatedOn: " + DateTime.Now.ToShortTimeString();

            this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Remove()
        {
            if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("Cookie"))
            {
                HttpCookie cookie = this.ControllerContext.HttpContext.Request.Cookies["Cookie"];
                cookie.Expires = DateTime.Now.AddDays(-1);
                this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
            }
            return RedirectToAction("Index", "Home");
        }

    }


这篇关于如何在mvc4中创建多个cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆