Exemple #1
0
        // Internal for unit testing
        internal static ViewLocationCacheKey GenerateKey(
            ViewLocationExpanderContext context,
            bool copyViewExpanderValues)
        {
            var controller = RazorViewEngine.GetNormalizedRouteValue(
                context.ActionContext,
                RazorViewEngine.ControllerKey);

            var area = RazorViewEngine.GetNormalizedRouteValue(
                context.ActionContext,
                RazorViewEngine.AreaKey);


            var values = context.Values;

            if (values != null && copyViewExpanderValues)
            {
                // When performing a Get, avoid creating a copy of the values dictionary
                values = new Dictionary <string, string>(values, StringComparer.Ordinal);
            }

            return(new ViewLocationCacheKey(
                       context.ViewName,
                       controller,
                       area,
                       context.IsPartial,
                       values));
        }
        internal static string GenerateKey(ViewLocationExpanderContext context)
        {
            var keyBuilder  = new StringBuilder();
            var routeValues = context.ActionContext.RouteData.Values;
            var controller  = RazorViewEngine.GetNormalizedRouteValue(
                context.ActionContext,
                RazorViewEngine.ControllerKey);

            // format is "{viewName}:{isPartial}:{controllerName}:{areaName}:"
            keyBuilder.Append(context.ViewName)
            .Append(CacheKeySeparator)
            .Append(context.IsPartial ? 1 : 0)
            .Append(CacheKeySeparator)
            .Append(controller);

            var area = RazorViewEngine.GetNormalizedRouteValue(context.ActionContext, RazorViewEngine.AreaKey);

            if (!string.IsNullOrEmpty(area))
            {
                keyBuilder.Append(CacheKeySeparator)
                .Append(area);
            }

            if (context.Values != null)
            {
                var valuesDictionary = context.Values;
                foreach (var item in valuesDictionary.OrderBy(k => k.Key, StringComparer.Ordinal))
                {
                    keyBuilder.Append(CacheKeySeparator)
                    .Append(item.Key)
                    .Append(CacheKeySeparator)
                    .Append(item.Value);
                }
            }

            var cacheKey = keyBuilder.ToString();

            return(cacheKey);
        }