Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpContext.Current.Request.Cookies在重新加载后不加载值_C#_Cookies - Fatal编程技术网

C# HttpContext.Current.Request.Cookies在重新加载后不加载值

C# HttpContext.Current.Request.Cookies在重新加载后不加载值,c#,cookies,C#,Cookies,我正在一个多语言的C#网站上工作。我已经写了一个数据库语言文件​​和语言​​班级。在这个类中,我将所有字符串都放在适当的语言中。默认情况下,语言为荷兰语,除非有cookie。在使用language类之前,我让它以默认值aspx.cs编写,并请求cookie,如下所示: Context.Request.Cookies ["lancookie"]; 如果语言被更改,我会更改cookie并重新加载页面。 在我使用的语言类中: HttpContext.Current.Request.Cookies [

我正在一个多语言的C#网站上工作。我已经写了一个数据库语言文件​​和语言​​班级。在这个类中,我将所有字符串都放在适当的语言中。默认情况下,语言为荷兰语,除非有cookie。在使用language类之前,我让它以默认值aspx.cs编写,并请求cookie,如下所示:

Context.Request.Cookies ["lancookie"];
如果语言被更改,我会更改cookie并重新加载页面。 在我使用的语言类中:

HttpContext.Current.Request.Cookies ["lancookie"].Value;
如果我更改了语言,那么它也需要几分钟的时间才能加载。我该怎么做才能触发cookie

你必须使用

HttpContext.Current.Response.Cookies

设置一个新的。要清除cookie,您必须将其过期日期设置为过去。我不想说太多细节,因为这应该能回答你的问题:


旁白:如果您使用的是“现代”版本的Internet Explorer,您可以使用F12开发者工具查看浏览器接收和传输的cookie。(其他浏览器可能也有类似的工具,但可以相信它们能正常工作。)这就是我现在使用的代码。它有一个延迟。如果(HttpContext.Current.Request.cookie[“lancookie”].Value!=null){HttpContext.Current.Request.cookie[“lancookie”].Expires=DateTime.Now.AddDays(-1);}HttpCookie=new HttpCookie(“lancookie”){Value=lan,Expires=DateTime.Now.AddMonths(1)};HttpContext.Current.Response.Cookies.Add(cookie);重新加载时,在页面加载之前,cookie是旧值。但是在response.write页面加载中,cookie是一个很好的值。我发现,当我再次将其保存到language类时,cookie被直接读取到它的新值。这似乎是一个缓存问题。
    public class Language
    {

      public static string getLanCookie ()
      {
        lancookie string = string.Empty;
        if (HttpContext.Current.Request.Cookies ["lancookie"]. Value! = null)
        {
            lancookie HttpContext.Current.Request.Cookies = ["lancookie"]. Value;
        }
        else
        {
            lancookie = "Dutch";
        }
        lancookie return;
      }

       public static string language = getLanCookie ()
       public static string Home = Language ("Home", language);
       public static string end = Language ("The End", language);
       public static string Subject = Language ("Box", language);

   }