asp.net是有可能从一个静态类读取cookie的值? [英] asp.net is it possible to read the value of a cookie from a static class?

查看:149
本文介绍了asp.net是有可能从一个静态类读取cookie的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态属性的值存储在用户访问该网站的cookie最后一次的currentUser类。我希望能够从这个类读取cookie的值。可能吗?它看起来像Request.Cookies时只在网页中使用。什么,我试图做一个简化的版本是:

 类的currentUser
{
公共静态字符串MyField的
            {                得到
                {
                    返回Request.Cookies时[MyField的];
                }            }}

这是行不通的。我得到这个错误信息:名字请求没有在目前的情况下存在


解决方案

 使用的System.Web;
...
返回HttpContext.Current.Request.Cookies [MyField的];

I have a CurrentUser class with a static property whose value was stored in a cookie last time the user visited the website. I would like to be able to read the value of the cookie from this class. Is it possible? It looks like Request.Cookies is only available in web pages. A simplified version of what I'm trying to do is:

class CurrentUser
{
public static string MyField
            {

                get
                {
                    return Request.Cookies["MyField"];
                }

            }

}

This doesn't work. I get this error message: "the name 'Request' does not exist in the current context'.

解决方案

using System.Web;
...
return HttpContext.Current.Request.Cookies["MyField"];

这篇关于asp.net是有可能从一个静态类读取cookie的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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