From 67dcdbf8a1f4c1f45f8ce1a245e726bb14fb7ac9 Mon Sep 17 00:00:00 2001 From: sornaks Date: Tue, 3 Feb 2015 14:50:05 -0800 Subject: [PATCH] Issue #1669: Adding extension method ConfigureRouteOptions. --- .../ServiceCollectionExtensions.cs | 26 +++++++++++++ .../RouteOptionsTests.cs | 38 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/Microsoft.AspNet.Routing/ServiceCollectionExtensions.cs diff --git a/src/Microsoft.AspNet.Routing/ServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Routing/ServiceCollectionExtensions.cs new file mode 100644 index 0000000000..78cf76dff8 --- /dev/null +++ b/src/Microsoft.AspNet.Routing/ServiceCollectionExtensions.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Routing; + +namespace Microsoft.Framework.DependencyInjection +{ + /// + /// Contains extension methods to . + /// + public static class ServiceCollectionExtensions + { + /// + /// Configures a set of for the application. + /// + /// The services available in the application. + /// An action to configure the . + public static void ConfigureRouteOptions( + this IServiceCollection services, + [NotNull] Action setupAction) + { + services.Configure(setupAction); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Routing.Tests/RouteOptionsTests.cs b/test/Microsoft.AspNet.Routing.Tests/RouteOptionsTests.cs index e40efc734f..1f32439ac2 100644 --- a/test/Microsoft.AspNet.Routing.Tests/RouteOptionsTests.cs +++ b/test/Microsoft.AspNet.Routing.Tests/RouteOptionsTests.cs @@ -2,6 +2,11 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; +using Microsoft.AspNet.Http; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.DependencyInjection.Fallback; +using Microsoft.Framework.OptionsModel; using Xunit; namespace Microsoft.AspNet.Routing.Tests @@ -19,5 +24,38 @@ namespace Microsoft.AspNet.Routing.Tests Assert.Equal("The 'ConstraintMap' property of 'Microsoft.AspNet.Routing.RouteOptions' must not be null." + Environment.NewLine + "Parameter name: value", ex.Message); } + + [Fact] + public void ConfigureRouteOptions_ConfiguresOptionsProperly() + { + // Arrange + var services = new ServiceCollection().AddOptions(); + + // Act + services.ConfigureRouteOptions(options => options.ConstraintMap.Add("foo", typeof(TestRouteConstraint))); + var serviceProvider = services.BuildServiceProvider(); + + // Assert + var accessor = serviceProvider.GetRequiredService>(); + Assert.Equal("TestRouteConstraint", accessor.Options.ConstraintMap["foo"].Name); + } + + private class TestRouteConstraint : IRouteConstraint + { + public TestRouteConstraint(string pattern) + { + Pattern = pattern; + } + + public string Pattern { get; private set; } + public bool Match(HttpContext httpContext, + IRouter route, + string routeKey, + IDictionary values, + RouteDirection routeDirection) + { + throw new NotImplementedException(); + } + } } }