Esempio n. 1
0
        protected void RestoreSession()
        {
            string value = "";

            try
            {
                value = this.Request.Cookies[IN_SESSION_COOKIE_NAME];
            }
            catch (KeyNotFoundException)
            {
                value = "";
            }

            CurrentSession          = new SessionModel();
            CurrentSession["valid"] = false;

            if (String.IsNullOrEmpty(value))
            {
                // Сессия не устанвлена, т.е. пользователь не зарегистрирован
                CurrentSession.GUID = ImportFromAtlcomru.GetGUID();
                // Сделать сессии идентификатор.
                return;
            }
            try
            {
                CurrentSession = activeSessions[value];                 // По идее там будет где-то пользователь.
                object ouser = null;
                CurrentSession["valid"] = CurrentSession.TryGetValue("user", out ouser);
                // Сессия валидна, если оттуда можно вытащить пользователя.
                CurrentSession["valid"] = ouser != null;
                CurrentSession["GUID"]  = value;
                return;
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                // Беспонтовая сессия, нам оно не надо
                // Возвращяем невалидную сессию по умолчанию
                CurrentSession.GUID = ImportFromAtlcomru.GetGUID();
                // Пусть будет беспонтовая анонимная новая сессия.
            }
        }
Esempio n. 2
0
        public Nancy.Response Render(string templateFile,
                                     object context = null,                                      // Model
                                     object view    = null)                                      // View
        {
            string   templateString = "";
            Template template       = null;

            bool gotCache = false;

            if (Application.USE_TEMPLATE_CACHE)
            {
                gotCache = Application.templateCache.TryGetValue(templateFile, out template);
            }

            // В режиме отладки иногда удобно, если при каждом запросе шаблон заново верстается.
            // Не надо сервак перезапускать при изменении шаблона.

            if (!gotCache)
            {
                string filePath    = Path.Combine(Application.TEMPLATE_LOCATION, templateFile);
                string tempPath123 = Path.Combine(Application.TEMPLATE_LOCATION, "_!123!_").Replace("_!123!_", "");
                Console.WriteLine("Template Path:" + filePath);
                templateString = File.ReadAllText(filePath);
                templateString = templateString.Replace("@TEMPLATEDIR@", tempPath123);                  // Подстановка местораположения шаблонов в текст шаблона.
                template       = new Template(templateString);

                if (Application.USE_TEMPLATE_CACHE)
                {
                    Application.templateCache.Add(templateFile, template);
                }
            }

            var dict = new Dictionary <string, object>();

            if (this.Request != null)
            {
                Request = this.Request;
                dict.Add("request", Request);
            }
            if (context != null)
            {
                dict.Add("model", context);
            }

            if (view == null)
            {
                throw new RenderException("null view");
            }

            dict.Add("view", view);
            dict.Add("application", Application.APPLICATION);
            dict.Add("appview", new ApplicationView(Application.APPLICATION, CurrentSession));

            object       omessage = null;
            MessageModel message  = null;
            bool         bmsg     = CurrentSession.TryGetValue("message", out omessage);

            if (!bmsg)
            {
                message = new MessageModel();                 // Пустое сообщение.
            }
            else
            {
                message = (MessageModel)omessage;
            }

            IAgent user  = null;
            object ouser = null;
            bool   buser = CurrentSession.TryGetValue("user", out ouser);

            if (!buser)
            {
                user = new InvalidUser();
            }
            else
            {
                user = (IAgent)ouser;
            }

            dict.Add("message", message);
            dict.Add("user", user);
            dict.Add("nothing", "");

            string result = template.Render(dict);

            CurrentSession.Remove("message");
            return(InSession(result));
        }