コード例 #1
0
 /// <summary>
 /// SessionStorageID-String unique to post/retrieve the storage type
 /// </summary>
 /// <returns></returns>
 public static string GetSessionStorageID(this IPersistentController inst)
 {
     return(StorageImplementation.GetSessionStorageID(inst.GetType().Name));
 }
コード例 #2
0
 public void GetSessionStorageNameTest()
 {
     Assert.That(StorageImplementation.GetSessionStorageID(this.GetType().Name),
                 Is.EqualTo("_SESSIONSTORAGEID_StorageImplementationTest"));
 }
コード例 #3
0
        /// <summary>
        /// Custom Controller factory method returning a serialized object if available.
        /// </summary>
        /// <param name="actionContext"></param>
        /// <returns></returns>
        public object Create(ControllerContext actionContext)
        {
            object controller         = null;
            var    controllerTypeInfo = actionContext.ActionDescriptor.ControllerTypeInfo;
            var    controllerType     = controllerTypeInfo.AsType();
            var    storageID          = StorageImplementation.GetStorageID(controllerTypeInfo.Name);
            var    sessionStorageID   = StorageImplementation.GetSessionStorageID(controllerTypeInfo.Name);

            var storage = StorageImplementation.GetStorage(this.Configuration, this.HttpContext, sessionStorageID);

            StorageImplementation.ClearIfRequested(this.HttpContext, storage, storageID);

            Guid sessionOverride;
            Guid session;

            byte[] bytes;
            Func <byte[], byte[]> filter = null;

            // ---------- Direct GET request ?session= from the Database ----------
            if (this.HttpContext.Request.Method == WebRequestMethods.Http.Get &&
                Guid.TryParse(this.HttpContext.Request.Query["session"], out sessionOverride))
            {
                using (var db = new ASP_DBEntities())
                {
                    (bytes, filter) = StorageImplementation.DatabaseBytes(Configuration, HttpContext, storageID, sessionOverride);
                    controller      = DeserializeController(actionContext, controllerTypeInfo, controllerType, bytes, filter);
                }
            }
            else
            {
                // ---------- Load from ViewState ----------
                if (storage == Storage.ViewState &&
                    this.HttpContext.Request.Method == WebRequestMethods.Http.Post &&
                    this.HttpContext.Request.Form.ContainsKey(storageID))
                {
                    // input type=hidden from <input viewstate="@ViewBag.ViewState" />
                    var controllerString = this.HttpContext.Request.Form[storageID];
                    if (!String.IsNullOrEmpty(controllerString))
                    {
                        (bytes, filter) = StorageImplementation.ViewStateBytes(this.Configuration, controllerString);
                        controller      = DeserializeController(actionContext, controllerTypeInfo, controllerType, bytes, filter);
                    }
                }

                // ---------- Load from Session ----------
                else if (storage == Storage.Session &&
                         this.HttpContext.Session.TryGetValue(storageID, out bytes))
                {
                    controller = DeserializeController(actionContext, controllerTypeInfo, controllerType, bytes);
                }

                // ---------- Load from Database ----------
                else if (storage == Storage.Database &&
                         Guid.TryParse(this.HttpContext.Request.Cookies[storageID].FromCookieString()["session"], out session))
                {
                    (bytes, filter) = StorageImplementation.DatabaseBytes(Configuration, HttpContext, storageID, session);
                    controller      = DeserializeController(actionContext, controllerTypeInfo, controllerType, bytes, filter);
                    ((IPersistentController)controller).Session = session;
                }

                // ---------- Load from X-ViewState Header ----------
                else if (storage == Storage.Header &&
                         this.HttpContext.Request.Headers.ContainsKey(StorageImplementation.HeaderName))
                {
                    // input type=hidden from <input viewstate="@ViewBag.ViewState" />
                    var controllerString = this.HttpContext.Request.Headers[StorageImplementation.HeaderName];
                    if (!String.IsNullOrEmpty(controllerString))
                    {
                        (bytes, filter) = StorageImplementation.ViewStateBytes(this.Configuration, controllerString);
                        controller      = DeserializeController(actionContext, controllerTypeInfo, controllerType, bytes, filter);
                    }
                }

                if (controller == null)
                {
                    // ASP.NET Core implementation, no persistence, just return the new controller
                    controller = actionContext.HttpContext.RequestServices.GetService(controllerType);
                }
            }

            return(controller);
        }