读取c中的cookie# [英] Reading cookie in c#

查看:106
本文介绍了读取c中的cookie#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望访问我国际化网站的人能够更改语言。我认为最好的方法是存储在cookie中选择的语言 - 所以当页面加载时,它可以查找cookie中的首选语言。

I want someone who visits my internationalized site to be able to change the language. I thought best way would be to store the language chosen in a cookie - so when the page loads, it can look up the preferred language in the cookie.

写cookie是完美的,但我总是遇到错误,试图读它。
因为我是新的C#我有麻烦翻译错误视觉工作室在我身上。

Writing the cookie is going perfect, however I keep running into errors trying to read it. As I'm new to C# im having trouble translating the errors visual studio throws at me.

我想要一个cookie的值称为'UserSettings'一个称为lang的字符串。我试着这样:

I want the value of a cookie called 'UserSettings' in a string called lang. I'm trying like this:

string lang = Request.Cookies["UserSettings"].Value;

它引发的错误是:


对象引用未设置为对象的实例

Object reference not set to an instance of an object

我认为问题必须在

我也尝试通过执行

HttpCookie cookie = Request.Cookies["UserSettings"].Value;

但视觉工作室完全不喜欢这样。

but visual studio doesnt like this at all.

我做错了什么?我必须创建一个Request对象的实例吗?

What am I doing wrong? Do I have to make an instance of the Request object? Any help would be appreciated.

我正在关注这个主题的不同教程,但都会导致同样的错误。

I'm following different tutorials on this topic, but they all result in this same error.

编辑:我注意到我有

HttpCookie cookie = Request.Cookies["UserSettings"].Value;

我将其更改为:

HttpCookie cookie = Request.Cookies["UserSettings"]; 

但没有运气,它仍然无法工作。

but no luck, it still didn't work.

EDIT:这是我的cookie的创建方式

this is how my cookie is made

 public void Application_BeginRequest()
    {
        myCookie = new HttpCookie("UserSettings");
        myCookie.Value = "nl";
        myCookie.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(myCookie);
        hc = new HomeController();
    }

我的cookie是100%,im absolutly肯定,我可以看到Firefox-web开发人员。

My cookie is 100% there, im absolutly sure, i can see it in Firefox-web developer.

更新:这是我现在呼叫的确切代码

UPDATE: this is the exact code i'm calling now

        public string getLang()
    {
       // HttpCookie aCookie = Request.Cookies["UserSettings"];
       //  string lang = Server.HtmlEncode(aCookie.Value);
       //  if (lang != null)
       // {
       //      currentLanguage = lang;
       //  }
        return currentLanguage;
    }

这种方式我的代码编译,如果我取消注释我的代码,然后它doest @这一方法的第一行)

this way my code compiles, if I uncomment my code then it doest (error @ first line of this method)

推荐答案

这听起来像是不会设置cookie。在这种情况下,您需要检查:

It sounds like the cookie is never being set. In which case you need to check for this:

HttpCookie aCookie = Request.Cookies["UserSettings"];
if(aCookie != null) {
     object userSettings = aCookie.Value;
} else {
     //Cookie not set.
}

要设置Cookie:

HttpCookie cookie = new HttpCookie("UserSettings");

cookie["UserSettings"] = myUserSettingsObject;
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);

这是一篇好文章: http://www.java2s.com/Code/ASP/Session-Cookie/CreateandretrieveCookiedataC.htm

这篇关于读取c中的cookie#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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