Using new StringRouteConstraint for area constraint
This commit is contained in:
parent
663f6a1718
commit
6e5187c1ae
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Mvc.Core;
|
using Microsoft.AspNetCore.Mvc.Core;
|
||||||
using Microsoft.AspNetCore.Routing;
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
using Microsoft.AspNetCore.Routing.Constraints;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Builder
|
namespace Microsoft.AspNetCore.Builder
|
||||||
{
|
{
|
||||||
|
|
@ -131,7 +132,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
defaultsDictionary["area"] = defaultsDictionary["area"] ?? areaName;
|
defaultsDictionary["area"] = defaultsDictionary["area"] ?? areaName;
|
||||||
|
|
||||||
var constraintsDictionary = new RouteValueDictionary(constraints);
|
var constraintsDictionary = new RouteValueDictionary(constraints);
|
||||||
constraintsDictionary["area"] = constraintsDictionary["area"] ?? areaName;
|
constraintsDictionary["area"] = constraintsDictionary["area"] ?? new StringRouteConstraint(areaName);
|
||||||
|
|
||||||
routeBuilder.MapRoute(name, template, defaultsDictionary, constraintsDictionary, dataTokens);
|
routeBuilder.MapRoute(name, template, defaultsDictionary, constraintsDictionary, dataTokens);
|
||||||
return routeBuilder;
|
return routeBuilder;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Routing;
|
using Microsoft.AspNetCore.Routing;
|
||||||
using Microsoft.AspNetCore.Routing.Constraints;
|
using Microsoft.AspNetCore.Routing.Constraints;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
@ -33,7 +34,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
kvp =>
|
kvp =>
|
||||||
{
|
{
|
||||||
Assert.Equal(kvp.Key, "area");
|
Assert.Equal(kvp.Key, "area");
|
||||||
Assert.IsType<RegexRouteConstraint>(kvp.Value);
|
Assert.IsType<StringRouteConstraint>(kvp.Value);
|
||||||
});
|
});
|
||||||
Assert.Empty(route.DataTokens);
|
Assert.Empty(route.DataTokens);
|
||||||
Assert.Collection(
|
Assert.Collection(
|
||||||
|
|
@ -68,7 +69,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
kvp =>
|
kvp =>
|
||||||
{
|
{
|
||||||
Assert.Equal(kvp.Key, "area");
|
Assert.Equal(kvp.Key, "area");
|
||||||
Assert.IsType<RegexRouteConstraint>(kvp.Value);
|
Assert.IsType<StringRouteConstraint>(kvp.Value);
|
||||||
});
|
});
|
||||||
Assert.Empty(route.DataTokens);
|
Assert.Empty(route.DataTokens);
|
||||||
Assert.Collection(
|
Assert.Collection(
|
||||||
|
|
@ -109,7 +110,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
kvp =>
|
kvp =>
|
||||||
{
|
{
|
||||||
Assert.Equal(kvp.Key, "area");
|
Assert.Equal(kvp.Key, "area");
|
||||||
Assert.IsType<RegexRouteConstraint>(kvp.Value);
|
Assert.IsType<StringRouteConstraint>(kvp.Value);
|
||||||
},
|
},
|
||||||
kvp =>
|
kvp =>
|
||||||
{
|
{
|
||||||
|
|
@ -156,7 +157,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
kvp =>
|
kvp =>
|
||||||
{
|
{
|
||||||
Assert.Equal(kvp.Key, "area");
|
Assert.Equal(kvp.Key, "area");
|
||||||
Assert.IsType<RegexRouteConstraint>(kvp.Value);
|
Assert.IsType<StringRouteConstraint>(kvp.Value);
|
||||||
},
|
},
|
||||||
kvp =>
|
kvp =>
|
||||||
{
|
{
|
||||||
|
|
@ -227,6 +228,48 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MapAreaRoute_UsesPassedInAreaNameAsIs()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var builder = CreateRouteBuilder();
|
||||||
|
var areaName = "user.admin";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
builder.MapAreaRoute(name: null, areaName: areaName, template: "site/Admin/");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var route = Assert.IsType<Route>((Assert.Single(builder.Routes)));
|
||||||
|
|
||||||
|
Assert.Null(route.Name);
|
||||||
|
Assert.Equal("site/Admin/", route.RouteTemplate);
|
||||||
|
Assert.Collection(
|
||||||
|
route.Constraints.OrderBy(kvp => kvp.Key),
|
||||||
|
kvp =>
|
||||||
|
{
|
||||||
|
Assert.Equal(kvp.Key, "area");
|
||||||
|
Assert.IsType<StringRouteConstraint>(kvp.Value);
|
||||||
|
|
||||||
|
var values = new RouteValueDictionary(new { area = areaName });
|
||||||
|
var match = kvp.Value.Match(
|
||||||
|
httpContext: Mock.Of<HttpContext>(),
|
||||||
|
route: new Mock<IRouter>().Object,
|
||||||
|
routeKey: kvp.Key,
|
||||||
|
values: values,
|
||||||
|
routeDirection: RouteDirection.UrlGeneration);
|
||||||
|
|
||||||
|
Assert.True(match);
|
||||||
|
});
|
||||||
|
Assert.Empty(route.DataTokens);
|
||||||
|
Assert.Collection(
|
||||||
|
route.Defaults.OrderBy(kvp => kvp.Key),
|
||||||
|
kvp =>
|
||||||
|
{
|
||||||
|
Assert.Equal(kvp.Key, "area");
|
||||||
|
Assert.Equal(kvp.Value, areaName);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private IServiceProvider CreateServices()
|
private IServiceProvider CreateServices()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue