如何使用cookie与ASP.Net(C#)的属性 [英] How to use a cookie as a property with ASP.Net (c#)

查看:127
本文介绍了如何使用cookie与ASP.Net(C#)的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个属性页面的后端设置cookie,我得到了奇怪的结果,不知道我在做什么是使用cookies的正确方法。

反正我的code是下面,如果你对如何最好地处理这个或任何想法,它已经做了一个例子,我将是一个感恩的回应
谢谢你。

  INT CurrentID
    {
        得到
        {
            如果(Request.Cookies时[CurrentID] == NULL)
                Response.Cookies.Add(新的HttpCookie(CurrentID,0));
            返回Request.Cookies时[CurrentID] Value.AsID()。
        }
        组
        {
            如果(Request.Cookies时[CurrentID]!= NULL)
                Response.Cookies.Remove(CurrentID);
            Response.Cookies.Add(新的HttpCookie(CurrentID,value.ToString()));
        }
    }


解决方案

在取出时再加入一个cookie的反响不会从Reequest删除等添加具有相同名称的新的cookie,所以获取属性它的时候想通了得到了Cookie的第一个版本,这是一个旧值。

但无论如何,解决办法如下。

  INT CurrentID
    {
        得到
        {
            如果(Request.Cookies时[CurrentID]!= NULL)
            {
                返回Request.Cookies时[CurrentID] Value.AsID()。
            }
            其他
            {
                Response.Cookies.Add(新的HttpCookie(CurrentID,0));
                返回0;
            }
        }
        组
        {
            如果(Response.Cookies [CurrentID]!= NULL)
            {
                Response.Cookies.Remove(CurrentID);
                Request.Cookies.Remove(CurrentID);
            }
            Response.Cookies.Add(新的HttpCookie(CurrentID,value.ToString()));
        }
    }

I am trying to use a property in the back end of a page to set a cookie, I am getting strange results, not sure if what I am doing is the correct way to use cookies.

Anyway my code is below, if you have any ideas on how best to handle this or an example where it has been done already I would be grateful for a response Thanks.

    int CurrentID
    {
        get
        {
            if (Request.Cookies["CurrentID"] == null)
                Response.Cookies.Add(new HttpCookie("CurrentID", "0"));
            return Request.Cookies["CurrentID"].Value.AsID();
        }
        set
        {
            if (Request.Cookies["CurrentID"] != null)
                Response.Cookies.Remove("CurrentID");
            Response.Cookies.Add(new HttpCookie("CurrentID", value.ToString()));
        }
    }

解决方案

Figured out when removing then adding a cookie to Responce it does not remove from Reequest and so adds a new cookie with the same name, so when getting the property it got the first version of the cookie which was an old value.

Anyway the solution is as follows.

    int CurrentID
    {
        get
        {
            if (Request.Cookies["CurrentID"] != null)
            {
                return Request.Cookies["CurrentID"].Value.AsID();
            }
            else
            {
                Response.Cookies.Add(new HttpCookie("CurrentID", "0"));
                return 0;
            }
        }
        set
        {
            if (Response.Cookies["CurrentID"] != null)
            {
                Response.Cookies.Remove("CurrentID");
                Request.Cookies.Remove("CurrentID");
            }
            Response.Cookies.Add(new HttpCookie("CurrentID", value.ToString()));
        }
    }

这篇关于如何使用cookie与ASP.Net(C#)的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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