public void GetVirtualPath_GetsValuesInCaseInsensitiveWay(string controllerKey) { var route = new HttpRoute("{controller}"); var request = new HttpRequestMessage(); request.SetRouteData( new HttpRouteData( route, new HttpRouteValueDictionary() { { "controller", "Employees" } } ) ); var values = new HttpRouteValueDictionary() { { "httproute", true }, { controllerKey, "Customers" } }; IHttpVirtualPathData virtualPath = route.GetVirtualPath(request, values); Assert.NotNull(virtualPath); Assert.Equal("Customers", virtualPath.VirtualPath); }
private static string GetHttpRouteHelper(HttpControllerContext controllerContext, string routeName, IDictionary <string, object> routeValues) { if (routeValues == null) { // If no route values were passed in at all we have to create a new dictionary // so that we can add the extra "httproute" key. routeValues = new Dictionary <string, object>(); routeValues.Add(HttpRoute.HttpRouteKey, true); } else { if (!routeValues.ContainsKey(HttpRoute.HttpRouteKey)) { // Copy the dictionary so that we can add the extra "httproute" key used by all Web API routes to // disambiguate them from other MVC routes. routeValues = new Dictionary <string, object>(routeValues); routeValues.Add(HttpRoute.HttpRouteKey, true); } } IHttpVirtualPathData vpd = controllerContext.Configuration.Routes.GetVirtualPath( controllerContext: controllerContext, name: routeName, values: routeValues); if (vpd == null) { return(null); } return(vpd.VirtualPath); }
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { // Only perform URL generation if the "httproute" key was specified. This allows these // routes to be ignored when a regular MVC app tries to generate URLs. Without this special // key an HTTP route used for Web API would normally take over almost all the routes in a // typical app. if (!values.ContainsKey(HttpRouteKey)) { return(null); } // Remove the value from the collection so that it doesn't affect the generated URL RouteValueDictionary newValues = GetRouteDictionaryWithoutHttpRouteKey(values); if (HttpRoute is HostedHttpRoute) { return(base.GetVirtualPath(requestContext, newValues)); } else { // if user passed us a custom IHttpRoute, then we should invoke their function instead of the base HttpRequestMessage request = requestContext.HttpContext.GetOrCreateHttpRequestMessage(); IHttpVirtualPathData virtualPathData = HttpRoute.GetVirtualPath(request, values); return(virtualPathData == null ? null : new VirtualPathData(this, virtualPathData.VirtualPath)); } }
public void IgnoreRoute_GetVirtualPathReturnsNull() { // Arrange DomainHttpRoute route = new DomainHttpRoute("myDomain", "api/{controller}/{action}", new { controller = "SomeValue", action = "SomeAction" }); HostedHttpRouteCollection collection = new HostedHttpRouteCollection(new RouteCollection()); collection.IgnoreRoute("domainRoute", route.RouteTemplate); HttpRequestMessage request = CreateHttpRequestMessageWithContext(); HttpRouteValueDictionary routeValues = new HttpRouteValueDictionary() { { "controller", "controllerName" }, { "action", "actionName" }, { "httproute", true } }; request.SetRouteData(new HttpRouteData(route, routeValues)); // Act IHttpVirtualPathData httpvPathData = collection.GetVirtualPath(request, "domainRoute", routeValues); // Assert // Altough it contains the ignore route, GetVirtualPath from the ignored route will always return null. Assert.Equal(collection.Count, 1); Assert.Null(httpvPathData); }
public void CustomHttpRouteGetVitualPathRunsCustomHttpRoute() { // Arrange DomainHttpRoute route = new DomainHttpRoute( "myDomain", "api/{controller}/{action}", new { controller = "SomeValue", action = "SomeAction" } ); HostedHttpRouteCollection collection = new HostedHttpRouteCollection( new RouteCollection() ); collection.Add("domainRoute", route); HttpRequestMessage request = CreateHttpRequestMessageWithContext(); HttpRouteValueDictionary routeValues = new HttpRouteValueDictionary() { { "controller", "controllerName" }, { "action", "actionName" }, { "httproute", true } }; request.SetRouteData(new HttpRouteData(route, routeValues)); // Act IHttpVirtualPathData httpvPathData = collection.GetVirtualPath( request, "domainRoute", routeValues ); // Assert Assert.NotNull(httpvPathData); Assert.Equal("/api/controllerName/actionNameFromDomain", httpvPathData.VirtualPath); }
public static string HttpRouteUrl(this HttpRequestMessage request, HttpMethod method, IDictionary <string, object> routeValues) { if (routeValues == null) { throw new ArgumentNullException("routeValues"); } if (!routeValues.ContainsKey("controller")) { throw new ArgumentException("'controller' key must be provided", "routeValues"); } routeValues = new HttpRouteValueDictionary(routeValues); if (!routeValues.ContainsKey(HttpRouteKey)) { routeValues.Add(HttpRouteKey, true); } string controllerName = routeValues["controller"].ToString(); routeValues.Remove("controller"); string actionName = string.Empty; if (routeValues.ContainsKey("action")) { actionName = routeValues["action"].ToString(); routeValues.Remove("action"); } IHttpRoute[] matchedRoutes = request.GetConfiguration().Services .GetApiExplorer().ApiDescriptions .Where(x => x.ActionDescriptor.ControllerDescriptor.ControllerName.Equals(controllerName, StringComparison.OrdinalIgnoreCase)) .Where(x => x.ActionDescriptor.SupportedHttpMethods.Contains(method)) .Where(x => string.IsNullOrEmpty(actionName) || x.ActionDescriptor.ActionName.Equals(actionName, StringComparison.OrdinalIgnoreCase)) .Select(x => new { route = x.Route, matches = x.ActionDescriptor.GetParameters() .Count(p => (!p.IsOptional) && (p.ParameterType.IsPrimitive || SimpleTypes.Contains(p.ParameterType)) && (routeValues.ContainsKey(p.ParameterName)) && (routeValues[p.ParameterName].GetType() == p.ParameterType)) }) .Where(x => x.matches > 0) .OrderBy(x => x.route.DataTokens["order"]) .ThenBy(x => x.route.DataTokens["precedence"]) .ThenByDescending(x => x.matches) .Select(x => x.route) .ToArray(); if (matchedRoutes.Length > 0) { IHttpVirtualPathData pathData = matchedRoutes[0].GetVirtualPath(request, routeValues); if (pathData != null) { return(pathData.VirtualPath); } } return(null); }
public void IgnoreRouteInternalNeverMatchesUrlGeneration() { // Arrange HttpRouteCollection routes = new HttpRouteCollection(); IHttpRoute route = routes.IgnoreRoute("Foo", "SomeRouteTemplate"); // Act IHttpVirtualPathData vpd = route.GetVirtualPath(new HttpRequestMessage(HttpMethod.Get, "SomeRouteTemplate"), null); // Assert Assert.Null(vpd); }
public void GetVirtualPath_GeneratesPathWithoutRouteData() { var route = new HttpRoute("{controller}"); var request = new HttpRequestMessage(); var values = new HttpRouteValueDictionary() { { "httproute", true }, { "controller", "Customers" } }; IHttpVirtualPathData virtualPath = route.GetVirtualPath(request, values); Assert.NotNull(virtualPath); Assert.Equal("Customers", virtualPath.VirtualPath); }
public void GenerateRoute_GetVirtualPathIsForwarded() { HttpRequestMessage request = new HttpRequestMessage(); IDictionary <string, object> values = new Dictionary <string, object>(); IHttpVirtualPathData data = new Mock <IHttpVirtualPathData>().Object; Mock <IHttpRoute> inner = new Mock <IHttpRoute>(); inner.Setup(r => r.GetVirtualPath(request, values)).Returns(data); LinkGenerationRoute route = new LinkGenerationRoute(inner.Object); IHttpVirtualPathData result = route.GetVirtualPath(request, values); Assert.Equal(data, result); }
private static string GetVirtualPath( HttpRequestMessage request, string routeName, IDictionary <string, object> routeValues ) { if (routeValues == null) { // If no route values were passed in at all we have to create a new dictionary // so that we can add the extra "httproute" key. routeValues = new HttpRouteValueDictionary(); routeValues.Add(HttpRoute.HttpRouteKey, true); } else { // Copy the dictionary so that we can guarantee that routeValues uses an OrdinalIgnoreCase comparer // and to add the extra "httproute" key used by all Web API routes to disambiguate them from other MVC routes. routeValues = new HttpRouteValueDictionary(routeValues); if (!routeValues.ContainsKey(HttpRoute.HttpRouteKey)) { routeValues.Add(HttpRoute.HttpRouteKey, true); } } HttpConfiguration configuration = request.GetConfiguration(); if (configuration == null) { throw Error.InvalidOperation( SRResources.HttpRequestMessageExtensions_NoConfiguration ); } IHttpVirtualPathData vpd = configuration.Routes.GetVirtualPath( request: request, name: routeName, values: routeValues ); if (vpd == null) { return(null); } return(vpd.VirtualPath); }
public virtual IHttpVirtualPathData GetVirtualPath( HttpRequestMessage request, string name, IDictionary <string, object> values ) { if (request == null) { throw Error.ArgumentNull("request"); } if (name == null) { throw Error.ArgumentNull("name"); } IHttpRoute route; if (!_dictionary.TryGetValue(name, out route)) { throw Error.Argument("name", SRResources.RouteCollection_NameNotFound, name); } IHttpVirtualPathData virtualPath = route.GetVirtualPath(request, values); if (virtualPath == null) { return(null); } // Construct a new VirtualPathData with the resolved app path string virtualPathRoot = GetVirtualPathRoot(request.GetRequestContext()); if (!virtualPathRoot.EndsWith("/", StringComparison.Ordinal)) { virtualPathRoot += "/"; } // Note: The virtual path root here always ends with a "/" and the // virtual path never starts with a "/" (that's how routes work). return(new HttpVirtualPathData( virtualPath.Route, virtualPathRoot + virtualPath.VirtualPath )); }
public void GetVirtualPath_ReturnsVirtualPathData() { var request = new HttpRequestMessage(); request.Properties[HttpControllerHandler.HttpContextBaseKey] = CreateHttpContext("~/api", "APP PATH MODIFIER RETURN VALUE"); var config = new HttpConfiguration(_webApiRoutes); IHttpRoute route = _webApiRoutes.CreateRoute("api", null, null); _webApiRoutes.Add("default", route); request.Properties[HttpPropertyKeys.HttpRouteDataKey] = _webApiRoutes.GetRouteData(request); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; IHttpVirtualPathData result = _webApiRoutes.GetVirtualPath(request, null, new HttpRouteValueDictionary { { "httproute", true } }); Assert.NotNull(result); Assert.Same(route, result.Route); Assert.Equal("APP PATH MODIFIER RETURN VALUE", result.VirtualPath); }
public override IHttpVirtualPathData GetVirtualPath(HttpRequestMessage request, IDictionary <string, object> values) { IHttpVirtualPathData virtualPath1 = base.GetVirtualPath(request, values); if (virtualPath1 != null) { string virtualPath2 = virtualPath1.VirtualPath; int num = virtualPath2.LastIndexOf("?", StringComparison.Ordinal); if (num != 0) { if (num > 0) { return(new HttpVirtualPathData(this, virtualPath2.Substring(0, num).ToLowerInvariant() + virtualPath2.Substring(num))); } return(new HttpVirtualPathData(this, virtualPath1.VirtualPath.ToLowerInvariant())); } } return(virtualPath1); }
public IHttpVirtualPathData GetVirtualPath(HttpRequestMessage request, IDictionary <string, object> values) { string currentCulture = Thread.CurrentThread.CurrentUICulture.Name; // If specific path is requested, override culture and remove RouteValue if (values.ContainsKey("culture")) { currentCulture = (string)values["culture"]; } IHttpRoute localizationRoute = GetLocalizedOrDefaultRoute(currentCulture); if (localizationRoute == null) { return(null); } // Get translated route from child route IHttpVirtualPathData pathData = localizationRoute.GetVirtualPath(request, CopyAndRemoveFromValueDictionary(values)); return(pathData); }
public void GetVirtualPath_OverriddenCultureByRouteDictionaryThatDoesNotExist_ReturnsNeutralLocalizationRoute() { using (ShimsContext.Create()) { // Arrange Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); LocalizationCollectionRoute localizationCollectionRoute = new LocalizationCollectionRoute(new HttpRoute("Home", new HttpRouteValueDictionary() { { "controller", "Home" }, { "action", "Index" } }, new HttpRouteValueDictionary(), new HttpRouteValueDictionary(), null)); PrivateObject privateObject = new PrivateObject(localizationCollectionRoute); IDictionary <string, LocalizationRoute> routes = (IDictionary <string, LocalizationRoute>)privateObject.GetProperty("LocalizedRoutesContainer"); ShimLocalizationRoute localizationRouteNeutral = new ShimLocalizationRoute(new LocalizationRoute("Welcome", new HttpRouteValueDictionary() { { "controller", "Home" }, { "action", "Index" } }, new HttpRouteValueDictionary(), new HttpRouteValueDictionary(), null, string.Empty)); (new ShimHttpRoute(localizationRouteNeutral)).GetVirtualPathHttpRequestMessageIDictionaryOfStringObject = (requestContext, values) => new HttpVirtualPathData(localizationRouteNeutral.Instance, "Welcome"); routes[string.Empty] = localizationRouteNeutral; ShimLocalizationRoute localizationRouteEnglish = new ShimLocalizationRoute(new LocalizationRoute("Welcome", new HttpRouteValueDictionary() { { "controller", "Home" }, { "action", "Index" } }, new HttpRouteValueDictionary(), new HttpRouteValueDictionary(), null, "en")); (new ShimHttpRoute(localizationRouteEnglish)).GetVirtualPathHttpRequestMessageIDictionaryOfStringObject = (requestContext, values) => new HttpVirtualPathData(localizationRouteEnglish.Instance, "Welcome"); routes["en"] = localizationRouteEnglish; ShimLocalizationRoute localizationRouteGerman = new ShimLocalizationRoute(new LocalizationRoute("Willkommen", new HttpRouteValueDictionary() { { "controller", "Home" }, { "action", "Index" } }, new HttpRouteValueDictionary(), new HttpRouteValueDictionary(), null, "de")); routes["de"] = localizationRouteGerman; (new ShimHttpRoute(localizationRouteGerman)).GetVirtualPathHttpRequestMessageIDictionaryOfStringObject = (requestContext, values) => new HttpVirtualPathData(localizationRouteGerman.Instance, "Willkommen"); // Act IHttpVirtualPathData virtualPathData = localizationCollectionRoute.GetVirtualPath(null, new HttpRouteValueDictionary() { { "Culture", "es" } }); // Assert Assert.AreEqual(virtualPathData.Route, localizationRouteNeutral.Instance); } }