/// <summary> /// Creates routes for the specified root controller and all other controllers /// in the same namespace or any sub-namespace, in the same assembly, and prepends the /// provided base route to the URL of each created route. /// </summary> /// <param name="configuration">The <see cref="System.Web.Http.HttpConfiguration"/> configuration object.</param> /// <param name="baseRoute">A base route to prepend to the URL of each created route. This parameter can be null.</param> /// <param name="rootController">The root controller for the provided base route.</param> /// <param name="settings">A settings object that customizes the route creation process. This parameter can be null.</param> /// <returns>The created routes.</returns> public static ICollection<IHttpRoute> MapCodeRoutes(this HttpConfiguration configuration, string baseRoute, Type rootController, CodeRoutingSettings settings) { if (configuration == null) throw new ArgumentNullException("configuration"); if (rootController == null) throw new ArgumentNullException("rootController"); if (settings != null) { settings = new CodeRoutingSettings(settings); } var registerSettings = new RegisterSettings(null, rootController) { BaseRoute = baseRoute, Settings = settings }; registerSettings.Settings.HttpConfiguration(configuration); IHttpRoute[] newRoutes = RouteFactory.CreateRoutes<IHttpRoute>(registerSettings); foreach (IHttpRoute route in newRoutes) { // In Web API v1 name cannot be null configuration.Routes.Add(Guid.NewGuid().ToString(), route); } EnableCodeRouting(configuration); return newRoutes; }
void Run(Type controller, CodeRoutingSettings settings = null, bool useBinder = false) { var config = new HttpConfiguration(); var routes = config.Routes; config.MapCodeRoutes(controller, settings); var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/02"); var routeData = routes.GetRouteData(request); var controllerInstance = (ApiController)Activator.CreateInstance(controller); var controllerContext = new HttpControllerContext(config, routeData, request) { ControllerDescriptor = new HttpControllerDescriptor(config, (string)routeData.Values["controller"], controller), Controller = controllerInstance }; object value; controllerInstance.ExecuteAsync(controllerContext, CancellationToken.None) .Result .TryGetContentValue(out value); string expected = (useBinder) ? "02" : "00"; Assert.AreEqual(expected, value.ToString()); }
void Run(Type controller, BinderPrecedence.BinderPrecedence expected, ParameterBinder globalBinder = null) { var settings = new CodeRoutingSettings(); if (globalBinder != null) { settings.ParameterBinders.Add(globalBinder); } routes.Clear(); routes.MapCodeRoutes(controller, settings); var httpContextMock = new Mock <HttpContextBase>(); httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/index/foo"); var httpResponseMock = new Mock <HttpResponseBase>(); httpContextMock.Setup(c => c.Response).Returns(httpResponseMock.Object); var routeData = routes.GetRouteData(httpContextMock.Object); var controllerInstance = (Controller)Activator.CreateInstance(controller); controllerInstance.ValidateRequest = false; var requestContext = new RequestContext(httpContextMock.Object, routeData); var controllerContext = new ControllerContext(requestContext, controllerInstance); controllerInstance.ValueProvider = new ValueProviderCollection(new IValueProvider[] { new RouteDataValueProvider(controllerContext) }); ((IController)controllerInstance).Execute(requestContext); httpResponseMock.Verify(c => c.Write(It.Is <string>(s => s == expected.ToString())), Times.AtLeastOnce()); }
void Run(Type controller, CodeRoutingSettings settings = null, bool useBinder = false) { if (settings == null) { settings = new CodeRoutingSettings(); } var routes = new RouteCollection(); routes.MapCodeRoutes(controller, settings); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Archive/02"); var httpResponseMock = new Mock<HttpResponseBase>(); httpContextMock.Setup(c => c.Response).Returns(httpResponseMock.Object); var routeData = routes.GetRouteData(httpContextMock.Object); var controllerInstance = (Controller)Activator.CreateInstance(controller); controllerInstance.ValidateRequest = false; var requestContext = new RequestContext(httpContextMock.Object, routeData); var controllerContext = new ControllerContext(requestContext, controllerInstance); controllerInstance.ValueProvider = new ValueProviderCollection(new IValueProvider[] { new RouteDataValueProvider(controllerContext) }); ((IController)controllerInstance).Execute(requestContext); string expected = (useBinder) ? "02" : "00"; httpResponseMock.Verify(c => c.Write(It.Is<string>(s => s == expected)), Times.AtLeastOnce()); }
void Run(Type controller, BinderPrecedence.BinderPrecedence expected, ParameterBinder globalBinder = null) { var settings = new CodeRoutingSettings(); if (globalBinder != null) { settings.ParameterBinders.Add(globalBinder); } config.MapCodeRoutes(controller, settings); var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/foo"); var routeData = routes.GetRouteData(request); var controllerInstance = (ApiController)Activator.CreateInstance(controller); var controllerContext = new HttpControllerContext(config, routeData, request) { ControllerDescriptor = new HttpControllerDescriptor(config, (string)routeData.Values["controller"], controller), Controller = controllerInstance }; string value; controllerInstance.ExecuteAsync(controllerContext, CancellationToken.None) .Result .TryGetContentValue(out value); Assert.AreEqual(expected.ToString(), value); }
public void NewInstanceCopiesValuesFromProvidedSettings() { var settings1 = new CodeRoutingSettings(); settings1.EnableEmbeddedViews = !settings1.EnableEmbeddedViews; var settings2 = new CodeRoutingSettings(settings1); Assert.AreEqual(settings1.EnableEmbeddedViews, settings2.EnableEmbeddedViews); }
public void RegexGlobalAndBinderType_DontUseBinder() { var settings = new CodeRoutingSettings { DefaultConstraints = { { typeof(RegexConstraint.MonthWithAssociatedBinder), "[0-9]{2}" } } }; Run(typeof(RegexConstraint.RegexGlobalAndBinderType.RegexConstraintController), settings); }
public void RegexParameterAndBinderGlobal_DontUseBinder() { var settings = new CodeRoutingSettings { ParameterBinders = { { new RegexConstraint.MonthParameterBinder() } } }; Run(typeof(RegexConstraint.RegexParameterAndBinderGlobal.RegexConstraintController), settings); }
public void RegexGlobalAndBinderParameter_UseBinder() { var settings = new CodeRoutingSettings { DefaultConstraints = { { typeof(RegexConstraint.Month), "[0-9]{2}" } } }; Run(typeof(RegexConstraint.RegexGlobalAndBinderParameter.RegexConstraintController), settings, useBinder: true); }
public void RegexGlobalAndBinderGlobal_DontUseBinder() { var settings = new CodeRoutingSettings { DefaultConstraints = { { typeof(RegexConstraint.Month), "[0-9]{2}" } }, ParameterBinders = { { new RegexConstraint.MonthParameterBinder() } } }; Run(typeof(RegexConstraint.RegexGlobalAndBinderGlobal.RegexConstraintController), settings); }
public void NewInstanceHasDefaultValues() { CodeRoutingSettings.Defaults.Reset(); var settings1 = new CodeRoutingSettings(); Assert.AreEqual(CodeRoutingSettings.Defaults.EnableEmbeddedViews, settings1.EnableEmbeddedViews); CodeRoutingSettings.Defaults.EnableEmbeddedViews = !CodeRoutingSettings.Defaults.EnableEmbeddedViews; var settings2 = new CodeRoutingSettings(); Assert.AreEqual(CodeRoutingSettings.Defaults.EnableEmbeddedViews, settings2.EnableEmbeddedViews); }
public void CollectionPropertiesAreNotWrapped() { // #747 // IgnoredControllers is wrapped by new CodeRoutingSettings instance instead of copying its items var settings1 = new CodeRoutingSettings { IgnoredControllers = { typeof(Settings.Settings1Controller) } }; var settings2 = new CodeRoutingSettings(settings1) { IgnoredControllers = { typeof(Settings.Settings2Controller) } }; Assert.IsFalse(Object.ReferenceEquals(settings1.IgnoredControllers, settings2.IgnoredControllers)); Assert.AreEqual(1, settings1.IgnoredControllers.Count); Assert.AreEqual(2, settings2.IgnoredControllers.Count); }
/// <summary> /// Creates routes for the specified root controller and all other controllers /// in the same namespace or any sub-namespace, in the same assembly, and prepends the /// provided base route to the URL of each created route. /// </summary> /// <param name="routes">A collection of routes for the application.</param> /// <param name="baseRoute">A base route to prepend to the URL of each created route. This parameter can be null.</param> /// <param name="rootController">The root controller for the provided base route.</param> /// <param name="settings">A settings object that customizes the route creation process. This parameter can be null.</param> /// <returns>The created routes.</returns> public static ICollection<Route> MapCodeRoutes(this RouteCollection routes, string baseRoute, Type rootController, CodeRoutingSettings settings) { if (routes == null) throw new ArgumentNullException("routes"); if (rootController == null) throw new ArgumentNullException("rootController"); var registerSettings = new RegisterSettings(null, rootController) { BaseRoute = baseRoute, Settings = settings }; Route[] newRoutes = RouteFactory.CreateRoutes<Route>(registerSettings); foreach (Route route in newRoutes) { routes.Add(route); } if (newRoutes.Length > 0 && registerSettings.Settings.EnableEmbeddedViews) { EmbeddedViewsVirtualPathProvider.RegisterAssembly(registerSettings); } return newRoutes; }
void Run(Type controller, CodeRoutingSettings settings = null, bool useBinder = false) { if (settings == null) { settings = new CodeRoutingSettings(); } var routes = new RouteCollection(); routes.MapCodeRoutes(controller, settings); var httpContextMock = new Mock <HttpContextBase>(); httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Archive/02"); var httpResponseMock = new Mock <HttpResponseBase>(); httpContextMock.Setup(c => c.Response).Returns(httpResponseMock.Object); var routeData = routes.GetRouteData(httpContextMock.Object); var controllerInstance = (Controller)Activator.CreateInstance(controller); controllerInstance.ValidateRequest = false; var requestContext = new RequestContext(httpContextMock.Object, routeData); var controllerContext = new ControllerContext(requestContext, controllerInstance); controllerInstance.ValueProvider = new ValueProviderCollection(new IValueProvider[] { new RouteDataValueProvider(controllerContext) }); ((IController)controllerInstance).Execute(requestContext); string expected = (useBinder) ? "02" : "00"; httpResponseMock.Verify(c => c.Write(It.Is <string>(s => s == expected)), Times.AtLeastOnce()); }
/// <summary> /// Creates routes for the specified root controller and all other controllers /// in the same namespace or any sub-namespace, in the same assembly. /// </summary> /// <param name="routes">A collection of routes for the application.</param> /// <param name="rootController">The root controller for the application.</param> /// <param name="settings">A settings object that customizes the route creation process. This parameter can be null.</param> /// <returns>The created routes.</returns> public static ICollection<Route> MapCodeRoutes(this RouteCollection routes, Type rootController, CodeRoutingSettings settings) { return MapCodeRoutes(routes, null, rootController, settings); }
/// <summary> /// Creates routes for the specified root controller and all other controllers /// in the same namespace or any sub-namespace, in the same assembly. /// </summary> /// <param name="configuration">The <see cref="System.Web.Http.HttpConfiguration"/> configuration object.</param> /// <param name="rootController">The root controller for the application.</param> /// <param name="settings">A settings object that customizes the route creation process. This parameter can be null.</param> /// <returns>The created routes.</returns> public static ICollection<IHttpRoute> MapCodeRoutes(this HttpConfiguration configuration, Type rootController, CodeRoutingSettings settings) { return MapCodeRoutes(configuration, null, rootController, settings); }
void Run(Type controller, BinderPrecedence.BinderPrecedence expected, ParameterBinder globalBinder = null) { var settings = new CodeRoutingSettings(); if (globalBinder != null) { settings.ParameterBinders.Add(globalBinder); } routes.Clear(); routes.MapCodeRoutes(controller, settings); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/index/foo"); var httpResponseMock = new Mock<HttpResponseBase>(); httpContextMock.Setup(c => c.Response).Returns(httpResponseMock.Object); var routeData = routes.GetRouteData(httpContextMock.Object); var controllerInstance = (Controller)Activator.CreateInstance(controller); controllerInstance.ValidateRequest = false; var requestContext = new RequestContext(httpContextMock.Object, routeData); var controllerContext = new ControllerContext(requestContext, controllerInstance); controllerInstance.ValueProvider = new ValueProviderCollection(new IValueProvider[] { new RouteDataValueProvider(controllerContext) }); ((IController)controllerInstance).Execute(requestContext); httpResponseMock.Verify(c => c.Write(It.Is<string>(s => s == expected.ToString())), Times.AtLeastOnce()); }