Example #1
0
        private string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations, string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations, string ThemeName)
        {
            searchedLocations = _emptyLocations;

            if (String.IsNullOrEmpty(name))
            {
                return(String.Empty);
            }

            string areaName   = AreaHelpers.GetAreaName(controllerContext.RouteData);
            bool   usingAreas = !String.IsNullOrEmpty(areaName);
            List <ViewLocation> viewLocations = GetViewLocations(locations, (usingAreas) ? areaLocations : null);

            if (viewLocations.Count == 0)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                  "属性不能为空", locationsPropertyName));
            }

            bool   nameRepresentsPath = IsSpecificPath(name);
            string cacheKey           = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, areaName, ThemeName);

            if (useCache)
            {
                return(ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey));
            }

            return((nameRepresentsPath) ?
                   GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) :
                   GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, cacheKey, ref searchedLocations, ThemeName));
        }
Example #2
0
        private static bool MatchProvider(MvcRoute applyTo, RequestContext requestContext)
        {
            var area       = AreaHelpers.GetAreaName(requestContext.RouteData);
            var controller = requestContext.GetRequestValue("controller");
            var action     = requestContext.GetRequestValue("action");


            var matched = applyTo.Area.EqualsOrNullEmpty(area, StringComparison.OrdinalIgnoreCase) &&
                          applyTo.Controller.Equals(controller, StringComparison.OrdinalIgnoreCase) &&
                          applyTo.Action.EqualsOrNullEmpty(action, StringComparison.OrdinalIgnoreCase);

            if (matched && applyTo.RouteValues != null)
            {
                foreach (var item in applyTo.RouteValues)
                {
                    if (item.Value == null)
                    {
                        continue;
                    }
                    var routeValue = requestContext.GetRequestValue(item.Key);
                    if (routeValue == null)
                    {
                        matched = false;
                        break;
                    }
                    if (!item.Value.ToString().EqualsOrNullEmpty(routeValue.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        matched = false;
                        break;
                    }
                }
            }

            return(matched);
        }
Example #3
0
        private string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations,
                               string locationsPropertyName, string name, string controllerName, string cacheKeyPrefix, bool useCache,
                               out string[] searchedLocations)
        {
            searchedLocations = _emptyLocations;

            if (string.IsNullOrEmpty(name))
            {
                return(string.Empty);
            }

            var areaName      = AreaHelpers.GetAreaName(controllerContext.RouteData);
            var usingAreas    = !string.IsNullOrEmpty(areaName);
            var viewLocations = GetViewLocations(locations, usingAreas ? areaLocations : null);

            if (viewLocations.Count == 0)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  HotcakesResources.Common_PropertyCannotBeNullOrEmpty, locationsPropertyName));
            }

            var nameRepresentsPath = IsSpecificPath(name);
            var rootFolder         = HotcakesApplication.Current.ViewsVirtualPath;
            var cacheKey           = CreateCacheKey(cacheKeyPrefix, name, nameRepresentsPath ? string.Empty : controllerName,
                                                    areaName, rootFolder);

            if (useCache)
            {
                // Only look at cached display modes that can handle the context.
                var possibleDisplayModes =
                    DisplayModeProvider.GetAvailableDisplayModesForContext(controllerContext.HttpContext,
                                                                           controllerContext.DisplayMode);
                foreach (var displayMode in possibleDisplayModes)
                {
                    var cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext,
                                                                           AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId));

                    if (cachedLocation != null)
                    {
                        if (controllerContext.DisplayMode == null)
                        {
                            controllerContext.DisplayMode = displayMode;
                        }

                        return(cachedLocation);
                    }
                }

                // GetPath is called again without using the cache.
                return(null);
            }
            return(nameRepresentsPath
                ? GetPathFromSpecificName(controllerContext, name, rootFolder, cacheKey, ref searchedLocations)
                : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, rootFolder,
                                         cacheKey, ref searchedLocations));
        }
Example #4
0
        public virtual MenuItem Initialize(MenuItem menuItem, ControllerContext controllerContext)
        {
            var areaName = AreaHelpers.GetAreaName(controllerContext.RouteData);

            if (!string.IsNullOrEmpty(areaName) && menuItem.RouteValues != null)
            {
                menuItem.RouteValues["area"] = areaName;
            }
            if (!string.IsNullOrEmpty(menuItem.Area) && menuItem.RouteValues != null)
            {
                menuItem.RouteValues["area"] = menuItem.Area;
            }


            var isActive =
                GetIsActive(menuItem, controllerContext);

            menuItem.IsCurrent = isActive;

            foreach (var sub in menuItem.Items)
            {
                sub.Initialize(controllerContext);
                if (sub.IsActive)
                {
                    menuItem.IsCurrent = false;
                }
                isActive = isActive || sub.IsActive;
            }

            menuItem.IsActive = isActive;

            //if (!this.IsActive)
            //{
            //    this.IsActive = DefaultActive(controllerContext);
            //}


            if (menuItem.Visible != false)
            {
                var isVisible =
                    GetIsVisible(menuItem, controllerContext);

                if (string.IsNullOrEmpty(menuItem.Action) && menuItem.Items.Where(it => it.Visible == true).Count() == 0)
                {
                    isVisible = false;
                }

                menuItem.Visible = isVisible;
            }


            menuItem.Initialized = true;

            return(menuItem);
        }
Example #5
0
        public void GetAreaNameReturnsNullIfRouteNotAreaAware()
        {
            // Arrange
            Route route = new Route("the_url", new MvcRouteHandler());

            // Act
            string areaName = AreaHelpers.GetAreaName(route);

            // Assert
            Assert.Null(areaName);
        }
Example #6
0
        private static IEnumerable <IToolbarProvider> MatchProviders(IEnumerable <IToolbarProvider> tabProviders, RouteData route)
        {
            var area       = AreaHelpers.GetAreaName(route);
            var controller = route.Values["controller"].ToString();
            var action     = route.Values["action"].ToString();

            return(tabProviders.Where(it => it.ApplyTo == null ||
                                      it.ApplyTo.Any(at => at.Area.EqualsOrNullEmpty(area, StringComparison.OrdinalIgnoreCase) &&
                                                     at.Controller.Equals(controller, StringComparison.OrdinalIgnoreCase) &&
                                                     at.Action.EqualsOrNullEmpty(action, StringComparison.OrdinalIgnoreCase))));
        }
Example #7
0
        public void GetAreaNameFromIAreaAssociatedItem()
        {
            // Arrange
            CustomRouteWithArea route = new CustomRouteWithArea();

            // Act
            string areaName = AreaHelpers.GetAreaName(route);

            // Assert
            Assert.Equal("area_name", areaName);
        }
Example #8
0
 protected virtual bool GetIsActive(MenuItem menuItem, ControllerContext controllerContext)
 {
     if (!string.IsNullOrEmpty(menuItem.Area))
     {
         if (string.Compare(menuItem.Area, AreaHelpers.GetAreaName(controllerContext.RouteData)) != 0)
         {
             return(false);
         }
     }
     return(string.Compare(controllerContext.RouteData.Values["controller"].ToString(), menuItem.Controller, true) == 0);
 }
        private string GetPath(ControllerContext controllerContext, string[] areaLocations, string[] sharedLocations, string name, string controllerName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations)
        {
            searchedLocations = _emptyLocations;

            if (String.IsNullOrEmpty(name))
            {
                return(String.Empty);
            }

            var areaName = AreaHelpers.GetAreaName(controllerContext.RouteData);
            List <ViewLocation> viewLocations = GetViewLocations(sharedLocations, areaLocations);

            bool   nameRepresentsPath = IsSpecificPath(name);
            string cacheKey           = CreateCacheKey(cacheKeyPrefix, name, (nameRepresentsPath) ? String.Empty : controllerName, areaName);

            if (useCache)
            {
                // Only look at cached display modes that can handle the context.
                IEnumerable <IDisplayMode> possibleDisplayModes = DisplayModeProvider.GetAvailableDisplayModesForContext(controllerContext.HttpContext, controllerContext.DisplayMode);
                foreach (IDisplayMode displayMode in possibleDisplayModes)
                {
                    string cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId));

                    if (cachedLocation == null)
                    {
                        // If any matching display mode location is not in the cache, fall back to the uncached behavior, which will repopulate all of our caches.
                        return(null);
                    }

                    // A non-empty cachedLocation indicates that we have a matching file on disk. Return that result.
                    if (cachedLocation.Length > 0)
                    {
                        if (controllerContext.DisplayMode == null)
                        {
                            controllerContext.DisplayMode = displayMode;
                        }

                        return(cachedLocation);
                    }
                    // An empty cachedLocation value indicates that we don't have a matching file on disk. Keep going down the list of possible display modes.
                }

                // GetPath is called again without using the cache.
                return(null);
            }
            else
            {
                return(nameRepresentsPath
                    ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations)
                    : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, cacheKey, ref searchedLocations));
            }
        }
Example #10
0
        public void GetAreaNameFromAreaRouteCollectionRoute()
        {
            // Arrange
            RouteCollection         routes  = new RouteCollection();
            AreaRegistrationContext context = new AreaRegistrationContext("area_name", routes);
            Route route = context.MapRoute(null, "the_url");

            // Act
            string areaName = AreaHelpers.GetAreaName(route);

            // Assert
            Assert.Equal("area_name", areaName);
        }
Example #11
0
        public void GetAreaNameFromRouteData()
        {
            // Arrange
            RouteData routeData = new RouteData();

            routeData.DataTokens["area"] = "area_name";

            // Act
            string areaName = AreaHelpers.GetAreaName(routeData);

            // Assert
            Assert.Equal("area_name", areaName);
        }
Example #12
0
        public void GetAreaNameFromRouteDataFallsBackToRoute()
        {
            // Arrange
            RouteCollection         routes  = new RouteCollection();
            AreaRegistrationContext context = new AreaRegistrationContext("area_name", routes);
            Route     route     = context.MapRoute(null, "the_url");
            RouteData routeData = new RouteData(route, new MvcRouteHandler());

            // Act
            string areaName = AreaHelpers.GetAreaName(routeData);

            // Assert
            Assert.Equal("area_name", areaName);
        }
        protected override bool GetIsActive(MenuItem menuItem, ControllerContext controllerContext)
        {
            string areaName       = AreaHelpers.GetAreaName(controllerContext.RouteData);
            string controllerName = controllerContext.RouteData.Values.GetString("controller");
            string actionName     = controllerContext.RouteData.Values.GetString("action");

            if (menuItem.Area.Equals(areaName, StringComparison.OrdinalIgnoreCase) &&
                menuItem.Controller.Equals(controllerName, StringComparison.OrdinalIgnoreCase) &&
                menuItem.Action.Equals(actionName, StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            return(false);
        }
        public IEnumerable <T> Match <T>(IEnumerable <T> applyToItems, System.Web.Routing.RouteData route, string position = null)
            where T : IApplyTo
        {
            if (applyToItems == null)
            {
                return(new T[0]);
            }
            var area       = AreaHelpers.GetAreaName(route);
            var controller = route.Values["controller"].ToString();
            var action     = route.Values["action"].ToString();

            return(applyToItems.Where(it => ((string.IsNullOrEmpty(position) && string.IsNullOrEmpty(it.Position)) || position.EqualsOrNullEmpty(it.Position, StringComparison.OrdinalIgnoreCase)) &&
                                      (it.ApplyTo == null ||
                                       it.ApplyTo.Any(at => at.Area.EqualsOrNullEmpty(area, StringComparison.OrdinalIgnoreCase) &&
                                                      at.Controller.Equals(controller, StringComparison.OrdinalIgnoreCase) &&
                                                      at.Action.EqualsOrNullEmpty(action, StringComparison.OrdinalIgnoreCase)))).ToArray());
        }
        protected virtual bool GetIsActive(MenuItem menuItem, ControllerContext controllerContext)
        {
            if (!string.IsNullOrEmpty(menuItem.Area))
            {
                if (string.Compare(menuItem.Area, AreaHelpers.GetAreaName(controllerContext.RouteData)) != 0)
                {
                    return(false);
                }
            }
            var active = string.Compare(controllerContext.RouteData.Values["controller"].ToString(), menuItem.Controller, true) == 0;

            if (active && menuItem.ReadOnlyProperties != null)
            {
                var activeByAction = menuItem.ReadOnlyProperties["activeByAction"];
                if (!string.IsNullOrEmpty(activeByAction) && activeByAction.ToLower() == "true")
                {
                    active = string.Compare(controllerContext.RouteData.Values["action"].ToString(), menuItem.Action, true) == 0;
                }
            }
            return(active);
        }
Example #16
0
 /// <summary>
 /// Gets the name of the area.
 /// </summary>
 /// <param name="routeData">The route data.</param>
 /// <returns>Area name.</returns>
 public static string GetAreaName(this RouteData routeData)
 {
     return(AreaHelpers.GetAreaName(routeData));
 }
Example #17
0
        public void Index(string name, string version, string condition)
        {
            HttpResponseBase response = Response;

            WebResourcesSection section = ConfigurationManager.GetSection(AreaHelpers.GetAreaName(this.RouteData));

            if (section == null)
            {
                throw new HttpException(500, "Unable to find the web resource configuration.");
            }

            ReferenceElement settings = section.References[name];

            if (settings == null)
            {
                throw new HttpException(500, string.Format("Unable to find any matching web resource settings for {0}.", name));
            }

            Condition conditionInfo = new Condition
            {
                If = condition ?? string.Empty
            };

            // filter the files based on the condition (Action / If) passed in
            IList <FileInfoElement> files = new List <FileInfoElement>();

            foreach (FileInfoElement fileInfo in settings.Files)
            {
                if (fileInfo.If.Equals(conditionInfo.If))
                {
                    files.Add(fileInfo);
                }
            }

            // Ooutput Type
            response.ContentType = settings.MimeType;
            Stream output = response.OutputStream;

            // Compress
            if (section.Compress)
            {
                string acceptEncoding = Request.Headers["Accept-Encoding"];

                if (!string.IsNullOrEmpty(acceptEncoding))
                {
                    acceptEncoding = acceptEncoding.ToLowerInvariant();

                    if (acceptEncoding.Contains("gzip"))
                    {
                        response.AddHeader("Content-encoding", "gzip");
                        output = new GZipStream(output, CompressionMode.Compress);
                    }
                    else if (acceptEncoding.Contains("deflate"))
                    {
                        response.AddHeader("Content-encoding", "deflate");
                        output = new DeflateStream(output, CompressionMode.Compress);
                    }
                }
            }

            // Combine
            using (StreamWriter sw = new StreamWriter(output))
            {
                foreach (FileInfoElement fileInfo in files)
                {
                    string content = System.IO.File.ReadAllText(Server.MapPath(fileInfo.Filename));

                    switch (settings.MimeType)
                    {
                    case "text/css":
                        content = CSSMinify.Minify(Url, fileInfo.Filename, Request.Url.AbsolutePath, content);
                        break;

                    case "text/x-javascript":
                    case "text/javascript":
                    case "text/ecmascript":
                        if (section.Compact)
                        {
                            content = JSMinify.Minify(content);
                        }
                        break;
                    }
                    sw.WriteLine(content.Trim());
                }
            }

            // Cache

            if (section.CacheDuration > 0)
            {
                DateTime            timestamp = HttpContext.Timestamp;
                HttpCachePolicyBase cache     = response.Cache;
                int duration = section.CacheDuration;

                cache.SetCacheability(HttpCacheability.Public);
                cache.SetExpires(timestamp.AddSeconds(duration));
                cache.SetMaxAge(new TimeSpan(0, 0, duration));
                cache.SetValidUntilExpires(true);
                cache.SetLastModified(timestamp);
                cache.VaryByHeaders["Accept-Encoding"] = true;
                cache.VaryByParams["name"]             = true;
                cache.VaryByParams["version"]          = true;
                cache.VaryByParams["display"]          = true;
                cache.VaryByParams["condition"]        = true;
                cache.SetOmitVaryStar(true);
            }
        }
Example #18
0
 public static IHtmlString ExternalResources(this HtmlHelper htmlHelper, string name, RouteValueDictionary htmlAttributes)
 {
     return(ExternalResources(htmlHelper, AreaHelpers.GetAreaName(htmlHelper.ViewContext.RouteData), name, htmlAttributes));
 }
Example #19
0
        public static Menu BuildMenu(ControllerContext controllerContext)
        {
            string areaName = AreaHelpers.GetAreaName(controllerContext.RouteData);

            return(BuildMenu(controllerContext, areaName));
        }