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


C# RouteValueDictionary.Clear方法代码示例

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


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

示例1: GenerateKey

        public string GenerateKey(ControllerContext context, CacheSettings cacheSettings)
        {
            var actionName = context.RouteData.Values["action"].ToString();
            var controllerName = context.RouteData.Values["controller"].ToString();

            // remove controller, action and DictionaryValueProvider which is added by the framework for child actions
            var filteredRouteData = context.RouteData.Values.Where(x => x.Key.ToLowerInvariant() != "controller" &&
                                                                   x.Key.ToLowerInvariant() != "action" &&
                                                                   !(x.Value is DictionaryValueProvider<object>));

            var routeValues = new RouteValueDictionary(filteredRouteData.ToDictionary(x => x.Key.ToLowerInvariant(), x => x.Value));

            if (!context.IsChildAction)
            {
                // note that route values take priority over form values and form values take priority over query string values

                foreach (var formKey in context.HttpContext.Request.Form.AllKeys)
                {
                    if (!routeValues.ContainsKey(formKey.ToLowerInvariant()))
                    {
                        routeValues.Add(formKey.ToLowerInvariant(),
                                        context.HttpContext.Request.Form[formKey].ToLowerInvariant());
                    }
                }

                foreach (var queryStringKey in context.HttpContext.Request.QueryString.AllKeys)
                {
                    // queryStringKey is null if url has qs name without value. e.g. test.com?q
                    if (queryStringKey != null && !routeValues.ContainsKey(queryStringKey.ToLowerInvariant()))
                    {
                        routeValues.Add(queryStringKey.ToLowerInvariant(),
                                        context.HttpContext.Request.QueryString[queryStringKey].ToLowerInvariant());
                    }
                }
            }

            if (!string.IsNullOrEmpty(cacheSettings.VaryByParam))
            {
                if (cacheSettings.VaryByParam.ToLowerInvariant() == "none")
                {
                    routeValues.Clear();
                }
                else if (cacheSettings.VaryByParam != "*")
                {
                    var parameters = cacheSettings.VaryByParam.ToLowerInvariant().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    routeValues = new RouteValueDictionary(routeValues.Where(x => parameters.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value));
                }
            }

            if (!string.IsNullOrEmpty(cacheSettings.VaryByCustom))
            {
                routeValues.Add(cacheSettings.VaryByCustom.ToLowerInvariant(), context.HttpContext.ApplicationInstance.GetVaryByCustomString(HttpContext.Current, cacheSettings.VaryByCustom));
            }

            var key = _keyBuilder.BuildKey(controllerName, actionName, routeValues);

            return key;
        }
开发者ID:jalchr,项目名称:donutoutputcache,代码行数:58,代码来源:KeyGenerator.cs

示例2: GetActionPath_Action_RouteValues

		public void GetActionPath_Action_RouteValues ()
		{
			MetaModel m = Utils.CommonInitialize (true);
			var route = RouteTable.Routes[0] as MyDynamicDataRoute;

			MetaTable t = m.Tables[TestDataContext.TableFooWithDefaults];
			var values = new RouteValueDictionary ();

			// NO null check for the routeValues parameter _again_!
			AssertExtensions.Throws<NullReferenceException> (() => t.GetActionPath (PageAction.Details, (RouteValueDictionary) null), "#A1");
			Assert.AreEqual (Utils.BuildActionName (t, PageAction.Details), t.GetActionPath (PageAction.Details, values), "#A2");
			Assert.AreEqual (2, values.Count, "#A3");

			// GetActionPath does not check if the Action and Table keys are present in the dictionary...
			values.Clear ();
			values.Add ("Action", "something");
			AssertExtensions.Throws<ArgumentException> (() => {
				t.GetActionPath (PageAction.Details, values);
			}, "#B1");

			values.Clear ();
			values.Add ("Table", "else");
			AssertExtensions.Throws<ArgumentException> (() => {
				t.GetActionPath (PageAction.Details, values);
			}, "#B2");
		}
开发者ID:nobled,项目名称:mono,代码行数:26,代码来源:MetaTableTest.cs

示例3: TryParseRouteExpression

		public void TryParseRouteExpression ()
		{
			string routeName;
			var rvd = new RouteValueDictionary ();
			
			Assert.IsFalse (RouteUrlExpressionBuilder.TryParseRouteExpression (null, rvd, out routeName), "#A1-1");
			Assert.IsFalse (RouteUrlExpressionBuilder.TryParseRouteExpression (String.Empty, rvd, out routeName), "#A1-2");
			Assert.IsFalse (RouteUrlExpressionBuilder.TryParseRouteExpression ("route", rvd, out routeName), "#A1-3");
			Assert.IsTrue (RouteUrlExpressionBuilder.TryParseRouteExpression ("route=", rvd, out routeName), "#A1-4");
			Assert.AreEqual (1, rvd.Count, "#A1-4-1");
			Assert.AreEqual (String.Empty, rvd ["route"], "#A1-4-2");

			AssertExtensions.Throws<NullReferenceException> (() => {
				RouteUrlExpressionBuilder.TryParseRouteExpression ("foo=bar", null, out routeName);
			}, "#A1-5");

			rvd.Clear ();
			Assert.IsTrue (RouteUrlExpressionBuilder.TryParseRouteExpression ("foo=bar", rvd, out routeName), "#A2-1");
			Assert.AreEqual (1, rvd.Count, "#A2-2");
			Assert.AreEqual ("bar", rvd ["foo"], "#A2-3");
			Assert.IsNull (routeName, "#A2-4");

			rvd.Clear ();
			Assert.IsTrue (RouteUrlExpressionBuilder.TryParseRouteExpression ("routeName=route,foo=bar,baz=zonk", rvd, out routeName), "#A3-1");
			Assert.AreEqual (2, rvd.Count, "#A3-2");
			Assert.AreEqual ("bar", rvd ["foo"], "#A3-3");
			Assert.AreEqual ("zonk", rvd ["baz"], "#A3-3");
			Assert.IsNotNull (routeName, "#A3-5");
			Assert.AreEqual ("route", routeName, "#A3-6");

			rvd.Clear ();
			Assert.IsTrue (RouteUrlExpressionBuilder.TryParseRouteExpression ("  rOUteName=route      ,  foo=bar\t,   baz   =\t  zonk   \t ", rvd, out routeName), "#A4-1");
			Assert.AreEqual (2, rvd.Count, "#A4-2");
			Assert.AreEqual ("bar", rvd ["foo"], "#A4-3");
			Assert.AreEqual ("zonk", rvd ["baz"], "#A4-3");
			Assert.IsNotNull (routeName, "#A4-5");
			Assert.AreEqual ("route", routeName, "#A4-6");

			rvd.Clear ();
			Assert.IsFalse (RouteUrlExpressionBuilder.TryParseRouteExpression ("foo=bar,route,baz=zonk", rvd, out routeName), "#A5-1");
			Assert.AreEqual (1, rvd.Count, "#A5-2");
			Assert.AreEqual ("bar", rvd ["foo"], "#A5-3");

			rvd.Clear ();
			Assert.IsFalse (RouteUrlExpressionBuilder.TryParseRouteExpression ("foo=bar,route==,baz=zonk", rvd, out routeName), "#A6-1");
			Assert.AreEqual (1, rvd.Count, "#A6-2");
			Assert.AreEqual ("bar", rvd ["foo"], "#A6-3");

			rvd.Clear ();
			Assert.IsFalse (RouteUrlExpressionBuilder.TryParseRouteExpression ("route==", rvd, out routeName), "#A7-1");
			Assert.AreEqual (0, rvd.Count, "#A7-2");

			rvd.Clear ();
			Assert.IsFalse (RouteUrlExpressionBuilder.TryParseRouteExpression ("route==stuff", rvd, out routeName), "#A8-1");
			Assert.AreEqual (0, rvd.Count, "#A8-2");

			rvd.Clear ();
			Assert.IsTrue (RouteUrlExpressionBuilder.TryParseRouteExpression ("route=stuff1,route=stuff2", rvd, out routeName), "#A9-1");
			Assert.AreEqual (1, rvd.Count, "#A9-2");
			Assert.AreEqual ("stuff2", rvd ["route"], "#A9-3");

			rvd.Clear ();
			Assert.IsFalse (RouteUrlExpressionBuilder.TryParseRouteExpression ("=stuff", rvd, out routeName), "#A10-1");
			Assert.AreEqual (0, rvd.Count, "#A10-2");

			rvd.Clear ();
			Assert.IsTrue (RouteUrlExpressionBuilder.TryParseRouteExpression ("routeName=route,routename=route2,foo=bar,baz=zonk", rvd, out routeName), "#A11-1");
			Assert.AreEqual (2, rvd.Count, "#A11-2");
			Assert.AreEqual ("bar", rvd ["foo"], "#A11-3");
			Assert.AreEqual ("zonk", rvd ["baz"], "#A11-3");
			Assert.IsNotNull (routeName, "#A11-5");
			Assert.AreEqual ("route2", routeName, "#A11-6");
		}
开发者ID:Profit0004,项目名称:mono,代码行数:73,代码来源:RouteUrlExpressionBuilderTest.cs

示例4: GenerateKey


//.........这里部分代码省略.........
                routeData.Values[RouteDataKeyController] != null)
            {
                controllerName = routeData.Values[RouteDataKeyController].ToString();
            }

            if (string.IsNullOrEmpty(actionName) || string.IsNullOrEmpty(controllerName))
            {
                return null;
            }

            string areaName = null;

            if (routeData.DataTokens.ContainsKey(DataTokensKeyArea))
            {
                areaName = routeData.DataTokens[DataTokensKeyArea].ToString();
            }

            // remove controller, action and DictionaryValueProvider which is added by the framework for child actions
            var filteredRouteData = routeData.Values.Where(
                x => x.Key.ToLowerInvariant() != RouteDataKeyController && 
                     x.Key.ToLowerInvariant() != RouteDataKeyAction &&   
                     x.Key.ToLowerInvariant() != DataTokensKeyArea &&
                     !(x.Value is DictionaryValueProvider<object>)
            ).ToList();

            if (!string.IsNullOrWhiteSpace(areaName))
            {
                filteredRouteData.Add(new KeyValuePair<string, object>(DataTokensKeyArea, areaName));
            }

            var routeValues = new RouteValueDictionary(filteredRouteData.ToDictionary(x => x.Key.ToLowerInvariant(), x => x.Value));

            if (!context.IsChildAction)
            {
                // note that route values take priority over form values and form values take priority over query string values

                if ((cacheSettings.Options & OutputCacheOptions.IgnoreFormData) != OutputCacheOptions.IgnoreFormData)
                {
                    foreach (var formKey in context.HttpContext.Request.Form.AllKeys)
                    {
                        if (routeValues.ContainsKey(formKey.ToLowerInvariant()))
                        {
                            continue;
                        }

                        var item = context.HttpContext.Request.Form[formKey];
                        routeValues.Add(
                            formKey.ToLowerInvariant(),
                            item != null 
                                ? item.ToLowerInvariant() 
                                : string.Empty
                        );
                    }
                }

                if ((cacheSettings.Options & OutputCacheOptions.IgnoreQueryString) != OutputCacheOptions.IgnoreQueryString)
                {
                    foreach (var queryStringKey in context.HttpContext.Request.QueryString.AllKeys)
                    {
                        // queryStringKey is null if url has qs name without value. e.g. test.com?q
                        if (queryStringKey == null || routeValues.ContainsKey(queryStringKey.ToLowerInvariant()))
                        {
                            continue;
                        }

                        var item = context.HttpContext.Request.QueryString[queryStringKey];
                        routeValues.Add(
                            queryStringKey.ToLowerInvariant(),
                            item != null 
                                ? item.ToLowerInvariant() 
                                : string.Empty
                        );
                    }
                }
            }

            if (!string.IsNullOrEmpty(cacheSettings.VaryByParam))
            {
                if (cacheSettings.VaryByParam.ToLowerInvariant() == "none")
                {
                    routeValues.Clear();
                }
                else if (cacheSettings.VaryByParam != "*")
                {
                    var parameters = cacheSettings.VaryByParam.ToLowerInvariant().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    routeValues = new RouteValueDictionary(routeValues.Where(x => parameters.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value));
                }
            }

            if (!string.IsNullOrEmpty(cacheSettings.VaryByCustom))
            {
                // If there is an existing route value with the same key as varybycustom, we should overwrite it
                routeValues[cacheSettings.VaryByCustom.ToLowerInvariant()] =
                            context.HttpContext.ApplicationInstance.GetVaryByCustomString(HttpContext.Current, cacheSettings.VaryByCustom);
            }

            var key = _keyBuilder.BuildKey(controllerName, actionName, routeValues);

            return key;
        }
开发者ID:doronuziel71,项目名称:mvcdonutcaching,代码行数:101,代码来源:KeyGenerator.cs


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