{"id":221,"date":"2018-05-12T22:05:41","date_gmt":"2018-05-12T15:05:41","guid":{"rendered":"http:\/\/thietkewebdalat.net\/blog\/?p=221"},"modified":"2018-05-12T22:08:21","modified_gmt":"2018-05-12T15:08:21","slug":"session-trong-c","status":"publish","type":"post","link":"https:\/\/thietkewebdalat.net\/blog\/session-trong-c\/","title":{"rendered":"Session trong c#"},"content":{"rendered":"<p>Ch\u00e0o c\u00e1c b\u1ea1n<\/p>\n<p>C\u00f3 l\u1ebb ai c\u0169ng \u0111ang th\u1eafc m\u1eafc l\u00e0 khi m\u00ecnh \u0111\u0103ng nh\u1eadp th\u00ec l\u00e0m sao \u0111\u1ec3 l\u01b0u l\u1ea1i t\u00ean \u0111\u0103ng nh\u1eadp ph\u1ea3i kh\u00f4ng \u1ea1.H\u00f4m nay m\u00ecnh s\u1ebd h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n t\u1ea1o ra session l\u01b0u nh\u01b0 tr\u00ean web nh\u00e9 :<\/p>\n<p>\u0110\u00e2y l\u00e0 code v\u00ed d\u1ee5 c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 tham kh\u1ea3o v\u00e0 s\u1eed d\u1ee5ng nh\u00e9 :<\/p>\n<pre>public class Session\r\n    {\r\n        private Dictionary&lt;string, object&gt; _lst = new Dictionary&lt;string, object&gt;();\r\n        private void Add(string key, object value)\/\/t\u1ea1o m\u1edbi v\u00e0 ch\u1ec9nh s\u1eeda value \r\n        {\r\n            if (_lst.ContainsKey(key))\/\/ch\u1ed7 n\u00e0y l\u00e0 ki\u1ec3m tra xem c\u00f3 key \u0111\u00f3 ch\u01b0a n\u1ebfu \r\n                _lst[key] = value;\r\n            else\r\n                _lst.Add(key, value);\/\/c\u00f2n kh\u00f4ng c\u00f3 th\u00ec m\u00ecnh add v\u00e0o;\r\n        }\r\n        private object Get(string key)\/\/l\u1ea5y session theo key\r\n        {\r\n            if (_lst.ContainsKey(key))\r\n                return _lst[key];\r\n            else\r\n                return null;\r\n        }\r\n \r\n        public object this[string key] { \/\/ ch\u1ed7 n\u00e0y m\u00ecnh s\u1ebd gi\u1ea3i th\u00edch ph\u00eda d\u01b0\u1edbi\r\n                  set { Add(key, value); } \r\n                  get { return Get(key); } \r\n          }\r\n \r\n        public void Clear()\r\n        {\r\n            _lst.Clear();\/\/x\u00f3a d\u1eef li\u1ec7u trong list (x\u00f3a session)\r\n        }\r\n    }\r\n\r\n\r\n<\/pre>\n<p>V\u00e0 \u0111\u00e2y l\u00e0 c\u00e1ch s\u1eed d\u1ee5ng nh\u00e9 :<\/p>\n<pre>   Session _session = new Session();\r\n   _session[\"IDUser\"]=1;\/\/g\u00e1n gi\u00e1 tr\u1ecb\r\n   Console.WriteLine(_session[\"IDUser\"]);\/\/in gi\u00e1 tr\u1ecb ra\r\n<\/pre>\n<p>B\u00e2y gi\u1edd m\u00ecnh s\u1ebd th\u00edch c\u00e1i \u0111o\u1ea1n m\u00ecnh comment nh\u00e9 \u00fd ngh\u0129a c\u1ee7a n\u00f3 nh\u01b0 sau:<br \/>\nC\u00e1i \u0111\u00f3 ng\u01b0\u1eddi ta g\u1ecdi l\u00e0 l\u1eadp ch\u1ec9 m\u1ee5c (Indexers) l\u00e0 m\u1ed9t ti\u1ec7n \u00edch cho ph\u00e9p m\u00ecnh t\u1ea1o l\u1edbp (class),m\u1ed9t c\u1ea5u tr\u00fac(struct) ho\u1eb7c m\u1ed9t interface m\u00e0 m\u00ecnh c\u00f3 th\u1ec3 truy c\u1eadp nh\u01b0 m\u1ed9t m\u1ea3ng .M\u00ecnh l\u1ea5y v\u00ed d\u1ee5 cho d\u1ec5 hi\u1ec3u nh\u00e9 :<\/p>\n<p>M\u00ecnh c\u00f3 1 Class Temperature \u0111\u1ea1i di\u1ec7n cho nhi\u1ec7t \u0111\u1ed9,class n\u00e0y ghi l\u1ea1i nhi\u1ec7t \u0111\u1ed9 trong 10 \u0111i\u1ec3m kh\u00e1c nhau trong 24h .Class n\u00e0y ch\u1ee9a 1 m\u1ea3ng l\u00e0 Temp ,code nh\u01b0 sau :<\/p>\n<pre> Class Temperature\r\n{\r\n    \r\n    private float[] Temp = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, \r\n                                            61.3F, 65.9F, 62.1F, 59.2F, 57.5F };\r\n\r\n\r\n   \r\n    public float this[int index]\r\n    {\r\n        get\r\n        {\r\n            return Temp[index];\r\n        }\r\n\r\n        set\r\n        {\r\n            Temp[index] = value;\r\n        }\r\n    }\r\n}\r\nclass MainClass\r\n{\r\n    static void Main()\r\n    {\r\n        Temperature _temp = new Temperature();\r\n      \r\n       \r\n        for (int i = 0; i &lt; 10; i++)\r\n        {\r\n            Console.WriteLine(\"Temp #{0} = {1}\", i, _temp[i]);\r\n        }\r\n\r\n      \r\n\r\n    }\r\n}\r\n<\/pre>\n<p>C\u1ea3m \u01a1n c\u00e1c b\u1ea1n c\u00f3 v\u1ea5n \u0111\u1ec1 g\u00ec th\u00ec comment nh\u00e9<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ch\u00e0o c\u00e1c b\u1ea1n C\u00f3 l\u1ebb ai c\u0169ng \u0111ang th\u1eafc m\u1eafc l\u00e0 khi m\u00ecnh \u0111\u0103ng nh\u1eadp th\u00ec l\u00e0m sao \u0111\u1ec3 l\u01b0u l\u1ea1i t\u00ean \u0111\u0103ng nh\u1eadp ph\u1ea3i kh\u00f4ng \u1ea1.H\u00f4m nay m\u00ecnh s\u1ebd h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n t\u1ea1o ra session l\u01b0u nh\u01b0 tr\u00ean web nh\u00e9 : \u0110\u00e2y l\u00e0 code v\u00ed d\u1ee5 c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 tham kh\u1ea3o v\u00e0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":222,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","footnotes":""},"categories":[8,9],"tags":[54,53,52,55],"class_list":{"0":"post-221","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-net","8":"category-c","9":"tag-session-c","10":"tag-session-in-c","11":"tag-session-trong-c","12":"tag-session-winform"},"_links":{"self":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":2,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions\/224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media\/222"}],"wp:attachment":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}