public void GetNormalizedRouteValue_ReturnsRouteDataValue_IfRouteConstraintKeyHandlingIsCatchAll() { // Arrange var key = "some-key"; var actionDescriptor = new ActionDescriptor { RouteConstraints = new[] { RouteDataActionConstraint.CreateCatchAll(key) } }; var actionContext = new ActionContext { ActionDescriptor = actionDescriptor, RouteData = new RouteData() }; actionContext.RouteData.Values[key] = "route-value"; // Act var result = RazorViewEngine.GetNormalizedRouteValue(actionContext, key); // Assert Assert.Equal("route-value", result); }
public void GetNormalizedRouteValue_UsesRouteValueDefaults_IfAttributeRouted() { // Arrange var key = "some-key"; var actionDescriptor = new ActionDescriptor { AttributeRouteInfo = new AttributeRouteInfo(), }; actionDescriptor.RouteValueDefaults[key] = "Route-Value"; var actionContext = new ActionContext { ActionDescriptor = actionDescriptor, RouteData = new RouteData() }; actionContext.RouteData.Values[key] = "route-value"; // Act var result = RazorViewEngine.GetNormalizedRouteValue(actionContext, key); // Assert Assert.Equal("Route-Value", result); }
public void GetNormalizedRouteValue_ReturnsRouteValue_IfValueDoesNotMatchRouteConstraint() { // Arrange var key = "some-key"; var actionDescriptor = new ActionDescriptor { RouteConstraints = new[] { new RouteDataActionConstraint(key, "different-value") } }; var actionContext = new ActionContext { ActionDescriptor = actionDescriptor, RouteData = new RouteData() }; actionContext.RouteData.Values[key] = "route-value"; // Act var result = RazorViewEngine.GetNormalizedRouteValue(actionContext, key); // Assert Assert.Equal("route-value", result); }
public void GetNormalizedRouteValue_ReturnsNull_IfRouteConstraintKeyHandlingIsDeny() { // Arrange var key = "some-key"; var actionDescriptor = new ActionDescriptor { RouteConstraints = new[] { new RouteDataActionConstraint(key, routeValue: string.Empty) } }; var actionContext = new ActionContext { ActionDescriptor = actionDescriptor, RouteData = new RouteData() }; actionContext.RouteData.Values[key] = "route-value"; // Act var result = RazorViewEngine.GetNormalizedRouteValue(actionContext, key); // Assert Assert.Null(result); }
public void GetNormalizedRouteValue_ConvertsRouteValueToString() { using (new CultureReplacer()) { // Arrange var key = "some-key"; var actionDescriptor = new ActionDescriptor { AttributeRouteInfo = new AttributeRouteInfo(), }; var actionContext = new ActionContext { ActionDescriptor = actionDescriptor, RouteData = new RouteData() }; actionContext.RouteData.Values[key] = 43; // Act var result = RazorViewEngine.GetNormalizedRouteValue(actionContext, key); // Assert Assert.Equal("43", result); } }
public void GetNormalizedRouteValue_ReturnsValueFromRouteConstraints_IfKeyHandlingIsRequired() { // Arrange var key = "some-key"; var actionDescriptor = new ActionDescriptor { RouteConstraints = new[] { new RouteDataActionConstraint(key, "Route-Value") } }; var actionContext = new ActionContext { ActionDescriptor = actionDescriptor, RouteData = new RouteData() }; actionContext.RouteData.Values[key] = "route-value"; // Act var result = RazorViewEngine.GetNormalizedRouteValue(actionContext, key); // Assert Assert.Equal("Route-Value", result); }
public IEnumerable <string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable <string> viewLocations) { if (context.ActionContext.RouteData.Values.ContainsKey("subarea")) { var subArea = RazorViewEngine.GetNormalizedRouteValue(context.ActionContext, "subarea"); return(viewLocations.Prepend("/Areas/{2}/" + subArea + "/Views/{1}/{0}.cshtml")); } return(viewLocations); }
public IEnumerable <string> ExpandViewLocations( ViewLocationExpanderContext context, IEnumerable <string> viewLocations) { if (context.Values.ContainsKey(SchubertApplicationModeProvider.ModuleRouteKeyName)) { var moduleName = RazorViewEngine.GetNormalizedRouteValue(context.ActionContext, SchubertApplicationModeProvider.ModuleRouteKeyName); viewLocations = viewLocations.Select(lo => $"/Modules/{moduleName}/{lo.TrimStart('/')}"); viewLocations = viewLocations.Union(new string[] { "/Views/Shared/{0}.cshtml" }).ToArray(); } return(viewLocations); }
public static bool IsCurrentPage(this ViewContext context, string pageName) { string normalizedRouteValue = RazorViewEngine.GetNormalizedRouteValue(context, "page"); string expectedPageValue = normalizedRouteValue + "/" + pageName; RouteData routeData = context.HttpContext.GetRouteData(); var page = (string)routeData.Values["page"]; if (page == expectedPageValue) { return(true); } return(false); }
public IEnumerable <string> ExpandViewLocations( ViewLocationExpanderContext context, IEnumerable <string> viewLocations) { if (context.Values.ContainsKey(_subAreaKey)) { var subArea = RazorViewEngine.GetNormalizedRouteValue(context.ActionContext, _subAreaKey); var subareaViewLocations = new string[] { "/Areas/{2}/Areas/" + subArea + "/Views/{1}/{0}.cshtml" }; viewLocations = subareaViewLocations.Concat(viewLocations); } return(viewLocations); }
public IEnumerable <string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable <string> viewLocations) { //check if subarea key contain if (context.ActionContext.RouteData.Values.ContainsKey(_subArea)) { string subArea = RazorViewEngine.GetNormalizedRouteValue(context.ActionContext, _subArea); IEnumerable <string> subAreaViewLocation = new string[] { "/Areas/{2}/SubAreas/" + subArea + "/Views/{1}/{0}.cshtml" }; viewLocations = subAreaViewLocation.Concat(viewLocations); } return(viewLocations); }
public IEnumerable <string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable <string> viewLocations) { if (context.ActionContext.RouteData.Values.ContainsKey(_tenant)) { string tenantArea = RazorViewEngine.GetNormalizedRouteValue(context.ActionContext, _tenant); IEnumerable <string> tenantViewLocation = new string[] { // $"/Views/Tenants/{tenantArea}/Views/{{1}}/{{0}}.cshtml", $"/Views/Tenants/{tenantArea}/Views/Shared/{{0}}.cshtml", }; viewLocations = tenantViewLocation.Concat(viewLocations); } return(viewLocations); }
public IEnumerable <String> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable <String> locations) { if (RazorViewEngine.GetNormalizedRouteValue(context.ActionContext, "area") != null) { return(new[] { "/Views/{2}/Shared/{0}.cshtml", "/Views/{2}/{1}/{0}.cshtml", "/Views/Shared/{0}.cshtml" }); } return(new[] { "/Views/{1}/{0}.cshtml", "/Views/Shared/{0}.cshtml" }); }
/// <inheritdoc/> public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (viewName == null) { throw new ArgumentNullException(nameof(viewName)); } var controllerName = RazorViewEngine.GetNormalizedRouteValue(context, "controller"); var path = controllerName + "/" + viewName; if (!_views.TryGetValue(path, out var value)) { return(ViewEngineResult.NotFound(path, new[] { "EightyCompiledViews/" + path })); } return(ViewEngineResult.Found(path, value(_serviceProvider))); }
/// <summary> /// Gets the case-normalized route value for the specified route <paramref name="key"/>. /// </summary> /// <param name="context">The <see cref="ActionContext"/>.</param> /// <param name="key">The route key to lookup.</param> /// <returns>The value corresponding to the key.</returns> /// <remarks> /// The casing of a route value in <see cref="ActionContext.RouteData"/> is determined by the client. /// This making constructing paths for view locations in a case sensitive file system unreliable. Using the /// <see cref="Abstractions.ActionDescriptor.RouteValues"/> to get route values /// produces consistently cased results. /// </remarks> public static string GetNormalizedRouteValue(ActionContext context, string key) => RazorViewEngine.GetNormalizedRouteValue(context, key);