4

I am working on a multilingual C# website . I have written a databaselanguages File ​​and Languages ​​class. In this class I put all strings in the appropriate language. By default, the language is Dutch, unless there is a cookie. Before i was using the language class I had it written in default,aspx.cs and requested the cookie like this:

Context.Request.Cookies ["lancookie"];

If the language was changed, I changed the cookie and reload the page. In the language class I use:

HttpContext.Current.Request.Cookies ["lancookie"].Value;

If I change the language, then it takes a few minutes before it also simply loads. What can I do to trigger the 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);

   }
MethodMan
  • 18,625
  • 6
  • 34
  • 52
GMBrian
  • 917
  • 1
  • 10
  • 16
  • Aside: If you're using a "modern" version of Internet Explorer, you can use the F12 developer tools to view the cookies received and transmitted by the browser. (Other browsers probably have similar tools, but they can be trusted to work correctly.) – HABO Aug 07 '12 at 19:28

1 Answers1

2

You have to use

HttpContext.Current.Response.Cookies

to set a new one. To be able to clear a cookie you will have to set it's expiration date to be in the past. Won't go into too much detail, as this should answer your question:

When to use Request.Cookies over Response.Cookies?

Community
  • 1
  • 1
Donatas Bacius
  • 646
  • 6
  • 16
  • that is the code I use now. which has a delay. if (HttpContext.Current.Request.Cookies["lancookie"].Value != null) { HttpContext.Current.Request.Cookies["lancookie"].Expires = DateTime.Now.AddDays(-1); } HttpCookie cookie = new HttpCookie("lancookie") { Value = lan, Expires = DateTime.Now.AddMonths(1) }; HttpContext.Current.Response.Cookies.Add(cookie); – GMBrian Aug 08 '12 at 08:45
  • On reload, before the pageload the cookie is the old value. But on the response.write in the page load the cookie is the good value. – GMBrian Aug 08 '12 at 12:59
  • I found that when I save it the language class again, the cookie is read directly to its new value. It seems to be a caching problem. – GMBrian Aug 08 '12 at 16:14