当前位置: 首页>>代码示例>>C#>>正文


C# TempDataDictionary.Add方法代码示例

本文整理汇总了C#中TempDataDictionary.Add方法的典型用法代码示例。如果您正苦于以下问题:C# TempDataDictionary.Add方法的具体用法?C# TempDataDictionary.Add怎么用?C# TempDataDictionary.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TempDataDictionary的用法示例。


在下文中一共展示了TempDataDictionary.Add方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: LoadTempDataIgnoresNullResponseCookieDoesNotThrowException

        public void LoadTempDataIgnoresNullResponseCookieDoesNotThrowException() {
            HttpCookie cookie = new HttpCookie("__ControllerTempData");
            TempDataDictionary initialTempData = new TempDataDictionary();
            initialTempData.Add("WhatIsInHere?", "Stuff");
            cookie.Value = CookieTempDataProvider.SerializeToBase64EncodedString(initialTempData);
            var cookies = new HttpCookieCollection();
            cookies.Add(cookie);

            var requestMock = new Mock<HttpRequestBase>();
            requestMock.Expect(r => r.Cookies).Returns(cookies);

            var responseMock = new Mock<HttpResponseBase>();
            responseMock.Expect(r => r.Cookies).Returns((HttpCookieCollection)null);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Expect(c => c.Request).Returns(requestMock.Object);
            httpContextMock.Expect(c => c.Response).Returns(responseMock.Object);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);

            IDictionary<string, object> tempData = provider.LoadTempData(null /* controllerContext */);
            Assert.AreEqual("Stuff", tempData["WhatIsInHere?"]);
        }
开发者ID:ledgarl,项目名称:Samples,代码行数:23,代码来源:CookieTempDataProviderTest.cs

示例2: TempDataIsADictionary

        public void TempDataIsADictionary()
        {
            // Arrange
            TempDataDictionary tempData = new TempDataDictionary();

            // Act
            tempData["Key1"] = "Value1";
            tempData.Add("Key2", "Value2");
            ((ICollection<KeyValuePair<string, object>>)tempData).Add(new KeyValuePair<string,object>("Key3", "Value3"));

            // Assert (IDictionary)
            Assert.AreEqual(3, tempData.Count, "tempData should contain 3 items");
            Assert.IsTrue(tempData.Remove("Key1"), "The key should be present");
            Assert.IsFalse(tempData.Remove("Key4"), "The key should not be present");
            Assert.IsTrue(tempData.ContainsValue("Value2"), "The value should be present");
            Assert.IsFalse(tempData.ContainsValue("Value1"), "The value should not be present");
            Assert.IsNull(tempData["Key6"], "The key should not be present");

            IEnumerator tempDataEnumerator = tempData.GetEnumerator();
            tempDataEnumerator.Reset();
            while (tempDataEnumerator.MoveNext()) {
                KeyValuePair<string, object> pair = (KeyValuePair<string, object>)tempDataEnumerator.Current;
                Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair), "The key/value pair should be present");
            }

            // Assert (ICollection)
            foreach (string key in tempData.Keys) {
                Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(new KeyValuePair<string, object>(key, tempData[key])), "The key/value pair should be present");
            }

            foreach (string value in tempData.Values) {
                Assert.IsTrue(tempData.ContainsValue(value));
            }

            foreach (string key in ((IDictionary<string, object>)tempData).Keys) {
                Assert.IsTrue(tempData.ContainsKey(key), "The key should be present");
            }

            foreach (string value in ((IDictionary<string, object>)tempData).Values) {
                Assert.IsTrue(tempData.ContainsValue(value));
            }

            KeyValuePair<string, object>[] keyValuePairArray = new KeyValuePair<string, object>[tempData.Count];
            ((ICollection<KeyValuePair<string, object>>)tempData).CopyTo(keyValuePairArray, 0);

            Assert.IsFalse(((ICollection<KeyValuePair<string, object>>)tempData).IsReadOnly, "The dictionary should not be read-only.");

            Assert.IsFalse(((ICollection<KeyValuePair<string, object>>)tempData).Remove(new KeyValuePair<string, object>("Key5", "Value5")), "The key/value pair should not be present");

            IEnumerator<KeyValuePair<string, object>> keyValuePairEnumerator = ((ICollection<KeyValuePair<string, object>>)tempData).GetEnumerator();
            keyValuePairEnumerator.Reset();
            while (keyValuePairEnumerator.MoveNext()) {
                KeyValuePair<string, object> pair = keyValuePairEnumerator.Current;
                Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair), "The key/value pair should be present");
            }

            // Act
            tempData.Clear();

            // Assert
            Assert.AreEqual(0, tempData.Count, "tempData should not contain any items");

            IEnumerator y = ((IEnumerable)tempData).GetEnumerator();
            while (y.MoveNext()) {
                Assert.Fail("There should not be any elements in tempData");
            }
        }
开发者ID:davidalmas,项目名称:Samples,代码行数:67,代码来源:TempDataDictionaryTest.cs

示例3: TempDataIsADictionary

        public void TempDataIsADictionary()
        {
            // Arrange
            TempDataDictionary tempData = new TempDataDictionary();

            // Act
            tempData["Key1"] = "Value1";
            tempData.Add("Key2", "Value2");
            ((ICollection<KeyValuePair<string, object>>)tempData).Add(new KeyValuePair<string, object>("Key3", "Value3"));

            // Assert (IDictionary)
            Assert.Equal(3, tempData.Count);
            Assert.True(tempData.Remove("Key1"));
            Assert.False(tempData.Remove("Key4"));
            Assert.True(tempData.ContainsValue("Value2"));
            Assert.False(tempData.ContainsValue("Value1"));
            Assert.Null(tempData["Key6"]);

            IEnumerator tempDataEnumerator = tempData.GetEnumerator();
            tempDataEnumerator.Reset();
            while (tempDataEnumerator.MoveNext())
            {
                KeyValuePair<string, object> pair = (KeyValuePair<string, object>)tempDataEnumerator.Current;
                Assert.True(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair));
            }

            // Assert (ICollection)
            foreach (string key in tempData.Keys)
            {
                Assert.True(((ICollection<KeyValuePair<string, object>>)tempData).Contains(new KeyValuePair<string, object>(key, tempData[key])));
            }

            foreach (string value in tempData.Values)
            {
                Assert.True(tempData.ContainsValue(value));
            }

            foreach (string key in ((IDictionary<string, object>)tempData).Keys)
            {
                Assert.True(tempData.ContainsKey(key));
            }

            foreach (string value in ((IDictionary<string, object>)tempData).Values)
            {
                Assert.True(tempData.ContainsValue(value));
            }

            KeyValuePair<string, object>[] keyValuePairArray = new KeyValuePair<string, object>[tempData.Count];
            ((ICollection<KeyValuePair<string, object>>)tempData).CopyTo(keyValuePairArray, 0);

            Assert.False(((ICollection<KeyValuePair<string, object>>)tempData).IsReadOnly);

            Assert.False(((ICollection<KeyValuePair<string, object>>)tempData).Remove(new KeyValuePair<string, object>("Key5", "Value5")));

            IEnumerator<KeyValuePair<string, object>> keyValuePairEnumerator = ((ICollection<KeyValuePair<string, object>>)tempData).GetEnumerator();
            keyValuePairEnumerator.Reset();
            while (keyValuePairEnumerator.MoveNext())
            {
                KeyValuePair<string, object> pair = keyValuePairEnumerator.Current;
                Assert.True(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair));
            }

            // Act
            tempData.Clear();

            // Assert
            Assert.Empty(tempData);
        }
开发者ID:huangw-t,项目名称:aspnetwebstack,代码行数:68,代码来源:TempDataDictionaryTest.cs

示例4: CanLoadTempDataFromCookie

        public void CanLoadTempDataFromCookie() {
            var tempData = new TempDataDictionary();
            tempData.Add("abc", "easy as 123");
            tempData.Add("price", 1.234);
            string serializedTempData = CookieTempDataProvider.SerializeToBase64EncodedString(tempData);
            
            var cookies = new HttpCookieCollection();
            var httpCookie = new HttpCookie("__ControllerTempData");
            httpCookie.Value = serializedTempData;
            cookies.Add(httpCookie);

            var requestMock = new Mock<HttpRequestBase>();
            requestMock.Expect(r => r.Cookies).Returns(cookies);

            var responseMock = new Mock<HttpResponseBase>();
            responseMock.Expect(r => r.Cookies).Returns(cookies);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Expect(c => c.Request).Returns(requestMock.Object);
            httpContextMock.Expect(c => c.Response).Returns(responseMock.Object);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);
            IDictionary<string, object> loadedTempData = provider.LoadTempData(null /* controllerContext */);
            Assert.AreEqual(2, loadedTempData.Count);
            Assert.AreEqual("easy as 123", loadedTempData["abc"]);
            Assert.AreEqual(1.234, loadedTempData["price"]);
        }
开发者ID:ledgarl,项目名称:Samples,代码行数:27,代码来源:CookieTempDataProviderTest.cs

示例5: SaveTempDataStoresSerializedFormInCookie

        public void SaveTempDataStoresSerializedFormInCookie() {
            var cookies = new HttpCookieCollection();
            var responseMock = new Mock<HttpResponseBase>();
            responseMock.Expect(r => r.Cookies).Returns(cookies);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Expect(c => c.Response).Returns(responseMock.Object);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);
            var tempData = new TempDataDictionary();
            tempData.Add("Testing", "Turn it up to 11");
            tempData.Add("Testing2", 1.23);

            provider.SaveTempData(null, tempData);
            HttpCookie cookie = cookies["__ControllerTempData"];
            string serialized = cookie.Value;
            IDictionary<string, object> deserializedTempData = CookieTempDataProvider.DeserializeTempData(serialized);
            Assert.AreEqual("Turn it up to 11", deserializedTempData["Testing"]);
            Assert.AreEqual(1.23, deserializedTempData["Testing2"]);
        }
开发者ID:ledgarl,项目名称:Samples,代码行数:20,代码来源:CookieTempDataProviderTest.cs

示例6: TryLoadTempData

        private void TryLoadTempData(Controller controller)
        {
            try
            {
                if (controller != null && controller.ControllerContext != null && controller.Session != null && !controller.ControllerContext.IsChildAction)
                {
                    // Saving the current temp data.
                    var oldTempData = new TempDataDictionary();
                    foreach (var kv in controller.TempData)
                    {
                        oldTempData.Add(kv.Key, kv.Value);
                    }

                    // Loading the temp data removes all current temp data.
                    controller.TempData.Load(controller.ControllerContext, controller.TempDataProvider);

                    // Restoring the current temp data.
                    foreach (var kv in oldTempData)
                    {
                        controller.TempData[kv.Key] = kv.Value;
                    }
                }
            }
            catch (Exception ex)
            {
                var exceptionMessage = string.Format(
                    "Failed to Load TempData. Class: {0}, Method: {1}, Exception {2}, Stack Trace {3}",
                    this.GetType().Name,
                    System.Reflection.MethodInfo.GetCurrentMethod().Name,
                    ex.Message,
                    ex.StackTrace);

                Log.Write(exceptionMessage, ConfigurationPolicy.ErrorLog);
            }
        }
开发者ID:RJCuthbertson,项目名称:feather,代码行数:35,代码来源:FeatherActionInvoker.cs


注:本文中的TempDataDictionary.Add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。