WordPress Hosting
Free Photos
WordPress Themes

C#: Good usage of Session[""] -> SessionMannager

/* ES: Este snipet está hecho para centralizar y mejorar la utilización de sesiones en aplicaciones web. EN: This snippet is made to centralize and improve the use of sessions in web applications. Enviorement MVC and VisualStudio 2015 Full post http://radell.es/variables-sesion-usarlas-correctamente/ */ using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace SessionMannagerDemo.UtilMannager { public class SessionMannager { public const string CANDY = "candy"; #region Get and Set public static T GetValue<T>(string sessionKey) { object sessionValue = HttpContext.Current.Session[sessionKey]; if (sessionValue == null) return default(T); else return ((T)sessionValue); } public static void SetValue(string sessionKey, object value) { HttpContext.Current.Session[sessionKey] = value; } #endregion #region Properties public static int Candy { get { return GetValue<int>(CANDY); } set { SetValue(CANDY, value); } } #endregion } } /* -- -- How use it on any MVC Controller or Class. -- -- Como usarlo en cualquier Controller o Class en MVC /// Tengo 3 caramelos /// I have 3 Candys SessionMannager.Candy = 3; /// Le doy un caramelo a mi hermano /// I give 1 candy to my brother SessionMannager.Candy -= 1; /// Resultado /// Result Console.Write(SessionMannager.Candy.ToString()); */

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.