public IEnumerable<string> ExpandViewLocations( ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { yield return "~/UI/{1}/Views/{0}.cshtml"; yield return "~/UI/SharedViews/{0}.cshtml"; }
public void InvokingGetAfterSet_ReturnsCachedItem(ViewLocationExpanderContext context) { // Arrange var cache = new DefaultViewLocationCache(); var value = new ViewLocationCacheResult( Guid.NewGuid().ToString(), new[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }); // Act - 1 cache.Set(context, value); var result = cache.Get(context); // Assert - 1 Assert.Equal(value, result); // Act - 2 result = cache.Get(context); // Assert - 2 Assert.Equal(value, result); }
/// <inheritdoc /> public virtual IEnumerable<string> ExpandViewLocations( ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (viewLocations == null) { throw new ArgumentNullException(nameof(viewLocations)); } string value; context.Values.TryGetValue(ValueKey, out value); if (!string.IsNullOrEmpty(value)) { CultureInfo culture; try { culture = new CultureInfo(value); } catch (CultureNotFoundException) { return viewLocations; } return ExpandViewLocationsCore(viewLocations, culture); } return viewLocations; }
public void PopulateValues(ViewLocationExpanderContext context) { context.Values[THEME_KEY] = context.ActionContext.HttpContext.GetTenant<AppTenant>()?.Theme; context.Values[TENANT_KEY] = context.ActionContext.HttpContext.GetTenant<AppTenant>()?.Name.Replace(" ", "-"); }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { List<string> expandedViewLocations = new List<string>(); expandedViewLocations.AddRange(viewLocations); expandedViewLocations.Add("/Views/SomeExtraFolder/{1}/{0}.cshtml"); return expandedViewLocations; }
/// <inheritdoc /> public void PopulateValues(ViewLocationExpanderContext context) { var value = _valueFactory(context.ActionContext); if (!string.IsNullOrEmpty(value)) { context.Values[ValueKey] = value; } }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { return new[] { "/Areas/{2}/App/{1}/{0}.cshtml", "/Areas/{2}/App/{0}.cshtml", }; }
public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { if (context.IsPartial) { return ExpandViewLocationsCore(viewLocations); } return viewLocations; }
public void Get_GeneratesCacheKeyIfItemDoesNotExist(ViewLocationExpanderContext context) { // Arrange var cache = new DefaultViewLocationCache(); // Act var result = cache.Get(context); // Assert Assert.Null(result); }
/// <inheritdoc /> public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { string value; if (context.Values.TryGetValue(ValueKey, out value)) { return ExpandViewLocationsCore(viewLocations, value); } return viewLocations; }
public void Get_ReturnsNoneResultIfItemDoesNotExist(ViewLocationExpanderContext context) { // Arrange var cache = new DefaultViewLocationCache(); // Act var result = cache.Get(context); // Assert Assert.Equal(result, ViewLocationCacheResult.None); }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (viewLocations == null) { throw new ArgumentNullException(nameof(viewLocations)); } return ViewLocationFormats; }
/// <inheritdoc /> public void PopulateValues(ViewLocationExpanderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // Using CurrentUICulture so it loads the locale specific resources for the views. #if NET451 context.Values[ValueKey] = Thread.CurrentThread.CurrentUICulture.Name; #else context.Values[ValueKey] = CultureInfo.CurrentUICulture.Name; #endif }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { // we don't want to change layout pages & partials ... if (context.IsPartial) return viewLocations; var descriptor = (context.ActionContext.ActionDescriptor as ControllerActionDescriptor); if (descriptor == null) { return viewLocations; } if (descriptor.ControllerName == "Home" && context.ActionContext.ActionDescriptor.Name == "Contact" && context.ActionContext.HttpContext.Request.Query.ContainsKey("apple")) { return viewLocations.Select(x => x.Replace("{0}", "Special/{0}")); } return viewLocations; }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { if (Logger != null) { Logger.LogVerbose("ExpandViewLocations called"); } // default: // "/Views/{1}/{0}.cshtml" // also: // 0 = action // 1 = controller var routes = new string[] { "/Sections/{1}/Views/{0}.cshtml", "/Sections/Common/Views/{0}.cshtml" }; return routes; }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { string theme = null; if (context.Values.TryGetValue(THEME_KEY, out theme)) { IEnumerable<string> themeLocations = new[] { $"/Themes/{theme}/{{1}}/{{0}}.cshtml", $"/Themes/{theme}/Shared/{{0}}.cshtml" }; string tenant; if (context.Values.TryGetValue(TENANT_KEY, out tenant)) { themeLocations = ExpandTenantLocations(tenant, themeLocations); } viewLocations = themeLocations.Concat(viewLocations); } return viewLocations; }
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; }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { throw new NotImplementedException(); }
/// <inheritdoc /> public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { return ExpandViewLocationsCore(viewLocations); }
public void PopulateValues(ViewLocationExpanderContext context) { // do nothing.. not entirely needed for this }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { return viewLocations.MoveViewsIntoFeaturesFolder(); }
protected abstract IEnumerable<string> GetViewLocation(ViewLocationExpanderContext context, IEnumerable<string> viewLocations, string areaName);
public void PopulateValues(ViewLocationExpanderContext context) { // nothing to do here }
public void PopulateValues(ViewLocationExpanderContext context) { throw new NotImplementedException(); }
public void PopulateValues(ViewLocationExpanderContext context) { }
private ViewLocationCacheResult LocatePageFromViewLocations( ActionContext actionContext, string pageName, bool isMainPage) { var controllerName = GetNormalizedRouteValue(actionContext, ControllerKey); var areaName = GetNormalizedRouteValue(actionContext, AreaKey); var expanderContext = new ViewLocationExpanderContext( actionContext, pageName, controllerName, areaName, isMainPage); Dictionary<string, string> expanderValues = null; if (_viewLocationExpanders.Count > 0) { expanderValues = new Dictionary<string, string>(StringComparer.Ordinal); expanderContext.Values = expanderValues; // Perf: Avoid allocations for (var i = 0; i < _viewLocationExpanders.Count; i++) { _viewLocationExpanders[i].PopulateValues(expanderContext); } } var cacheKey = new ViewLocationCacheKey( expanderContext.ViewName, expanderContext.ControllerName, expanderContext.AreaName, expanderContext.IsMainPage, expanderValues); ViewLocationCacheResult cacheResult; if (!ViewLookupCache.TryGetValue(cacheKey, out cacheResult)) { _logger.ViewLookupCacheMiss(cacheKey.ViewName, cacheKey.ControllerName); cacheResult = OnCacheMiss(expanderContext, cacheKey); } else { _logger.ViewLookupCacheHit(cacheKey.ViewName, cacheKey.ControllerName); } return cacheResult; }
public void PopulateValues(ViewLocationExpanderContext context) { context.Values[AreaKey] = context.ActionContext.RouteData.Values.FirstOrDefault(x => x.Key.Equals(AreaKey)).Value.ToString(); }
private ViewLocationCacheResult OnCacheMiss( ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey) { // Only use the area view location formats if we have an area token. var viewLocations = !string.IsNullOrEmpty(expanderContext.AreaName) ? AreaViewLocationFormats : ViewLocationFormats; for (var i = 0; i < _viewLocationExpanders.Count; i++) { viewLocations = _viewLocationExpanders[i].ExpandViewLocations(expanderContext, viewLocations); } ViewLocationCacheResult cacheResult = null; var searchedLocations = new List<string>(); var expirationTokens = new HashSet<IChangeToken>(); foreach (var location in viewLocations) { var path = string.Format( CultureInfo.InvariantCulture, location, expanderContext.ViewName, expanderContext.ControllerName, expanderContext.AreaName); cacheResult = CreateCacheResult(expirationTokens, path, expanderContext.IsMainPage); if (cacheResult != null) { break; } searchedLocations.Add(path); } // No views were found at the specified location. Create a not found result. if (cacheResult == null) { cacheResult = new ViewLocationCacheResult(searchedLocations); } var cacheEntryOptions = new MemoryCacheEntryOptions(); cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration); foreach (var expirationToken in expirationTokens) { cacheEntryOptions.AddExpirationToken(expirationToken); } return ViewLookupCache.Set<ViewLocationCacheResult>(cacheKey, cacheResult, cacheEntryOptions); }
private RazorPageResult LocatePageFromViewLocations(ActionContext context, string pageName, bool isPartial) { // Initialize the dictionary for the typical case of having controller and action tokens. var areaName = GetNormalizedRouteValue(context, AreaKey); // Only use the area view location formats if we have an area token. var viewLocations = !string.IsNullOrEmpty(areaName) ? AreaViewLocationFormats : ViewLocationFormats; var expanderContext = new ViewLocationExpanderContext(context, pageName, isPartial); if (_viewLocationExpanders.Count > 0) { expanderContext.Values = new Dictionary<string, string>(StringComparer.Ordinal); // 1. Populate values from viewLocationExpanders. foreach (var expander in _viewLocationExpanders) { expander.PopulateValues(expanderContext); } } // 2. With the values that we've accumumlated so far, check if we have a cached result. var pageLocation = _viewLocationCache.Get(expanderContext); if (!string.IsNullOrEmpty(pageLocation)) { var page = _pageFactory.CreateInstance(pageLocation); if (page != null) { // 2a. We found a IRazorPage at the cached location. return new RazorPageResult(pageName, page); } } // 2b. We did not find a cached location or did not find a IRazorPage at the cached location. // The cached value has expired and we need to look up the page. foreach (var expander in _viewLocationExpanders) { viewLocations = expander.ExpandViewLocations(expanderContext, viewLocations); } // 3. Use the expanded locations to look up a page. var controllerName = GetNormalizedRouteValue(context, ControllerKey); var searchedLocations = new List<string>(); foreach (var path in viewLocations) { var transformedPath = string.Format(CultureInfo.InvariantCulture, path, pageName, controllerName, areaName); var page = _pageFactory.CreateInstance(transformedPath); if (page != null) { // 3a. We found a page. Cache the set of values that produced it and return a found result. _viewLocationCache.Set(expanderContext, transformedPath); return new RazorPageResult(pageName, page); } searchedLocations.Add(transformedPath); } // 3b. We did not find a page for any of the paths. return new RazorPageResult(pageName, searchedLocations); }
private RazorPageResult LocatePageFromViewLocations(ActionContext context, string pageName, bool isPartial) { // Initialize the dictionary for the typical case of having controller and action tokens. var areaName = GetNormalizedRouteValue(context, AreaKey); // Only use the area view location formats if we have an area token. var viewLocations = !string.IsNullOrEmpty(areaName) ? AreaViewLocationFormats : ViewLocationFormats; var expanderContext = new ViewLocationExpanderContext(context, pageName, isPartial); if (_viewLocationExpanders.Count > 0) { expanderContext.Values = new Dictionary <string, string>(StringComparer.Ordinal); // 1. Populate values from viewLocationExpanders. foreach (var expander in _viewLocationExpanders) { expander.PopulateValues(expanderContext); } } // 2. With the values that we've accumumlated so far, check if we have a cached result. var pageLocation = _viewLocationCache.Get(expanderContext); if (!string.IsNullOrEmpty(pageLocation)) { var page = _pageFactory.CreateInstance(pageLocation); if (page != null) { // 2a. We found a IRazorPage at the cached location. return(new RazorPageResult(pageName, page)); } } // 2b. We did not find a cached location or did not find a IRazorPage at the cached location. // The cached value has expired and we need to look up the page. foreach (var expander in _viewLocationExpanders) { viewLocations = expander.ExpandViewLocations(expanderContext, viewLocations); } // 3. Use the expanded locations to look up a page. var controllerName = GetNormalizedRouteValue(context, ControllerKey); var searchedLocations = new List <string>(); foreach (var path in viewLocations) { var transformedPath = string.Format(CultureInfo.InvariantCulture, path, pageName, controllerName, areaName); var page = _pageFactory.CreateInstance(transformedPath); if (page != null) { // 3a. We found a page. Cache the set of values that produced it and return a found result. _viewLocationCache.Set(expanderContext, transformedPath); return(new RazorPageResult(pageName, page)); } searchedLocations.Add(transformedPath); } // 3b. We did not find a page for any of the paths. return(new RazorPageResult(pageName, searchedLocations)); }
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { var areaName = context.Values[AreaKey]; return CurrentArea != areaName ? viewLocations : GetViewLocation(context, viewLocations, areaName); }