Issue #1669: Adding extension method ConfigureRouteOptions.

This commit is contained in:
sornaks 2015-02-03 14:50:05 -08:00
parent c59366e8a7
commit 67dcdbf8a1
2 changed files with 64 additions and 0 deletions

View File

@ -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
{
/// <summary>
/// Contains extension methods to <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Configures a set of <see cref="RouteOptions"/> for the application.
/// </summary>
/// <param name="services">The services available in the application.</param>
/// <param name="setupAction">An action to configure the <see cref="RouteOptions"/>.</param>
public static void ConfigureRouteOptions(
this IServiceCollection services,
[NotNull] Action<RouteOptions> setupAction)
{
services.Configure(setupAction);
}
}
}

View File

@ -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<IOptions<RouteOptions>>();
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<string, object> values,
RouteDirection routeDirection)
{
throw new NotImplementedException();
}
}
}
}