React to Routing repo's api changes

This commit is contained in:
Kiran Challa 2018-08-03 13:18:32 -07:00
parent d4472f08ed
commit c16f86f0ef
14 changed files with 48 additions and 103 deletions

View File

@ -39,7 +39,7 @@
<MicrosoftAspNetCoreHttpPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreHttpPackageVersion>
<MicrosoftAspNetCoreJsonPatchPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreJsonPatchPackageVersion>
<MicrosoftAspNetCoreLocalizationPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreLocalizationPackageVersion>
<MicrosoftAspNetCoreLocalizationRoutingPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreLocalizationRoutingPackageVersion>
<MicrosoftAspNetCoreLocalizationRoutingPackageVersion>2.2.0-a-preview1-routing-lg-16736</MicrosoftAspNetCoreLocalizationRoutingPackageVersion>
<MicrosoftAspNetCoreMvcRazorExtensionsPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreMvcRazorExtensionsPackageVersion>
<MicrosoftAspNetCoreRangeHelperSourcesPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreRangeHelperSourcesPackageVersion>
<MicrosoftAspNetCoreRazorDesignPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreRazorDesignPackageVersion>
@ -48,8 +48,8 @@
<MicrosoftAspNetCoreRazorTagHelpersTestingSourcesPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreRazorTagHelpersTestingSourcesPackageVersion>
<MicrosoftAspNetCoreResponseCachingAbstractionsPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreResponseCachingAbstractionsPackageVersion>
<MicrosoftAspNetCoreResponseCachingPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreResponseCachingPackageVersion>
<MicrosoftAspNetCoreRoutingAbstractionsPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreRoutingAbstractionsPackageVersion>
<MicrosoftAspNetCoreRoutingPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreRoutingPackageVersion>
<MicrosoftAspNetCoreRoutingAbstractionsPackageVersion>2.2.0-a-preview1-change-linkgenerator-api-16845</MicrosoftAspNetCoreRoutingAbstractionsPackageVersion>
<MicrosoftAspNetCoreRoutingPackageVersion>2.2.0-a-preview1-change-linkgenerator-api-16845</MicrosoftAspNetCoreRoutingPackageVersion>
<MicrosoftAspNetCoreServerIISIntegrationPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreServerIISIntegrationPackageVersion>
<MicrosoftAspNetCoreServerKestrelPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreServerKestrelPackageVersion>
<MicrosoftAspNetCoreSessionPackageVersion>2.2.0-preview1-34869</MicrosoftAspNetCoreSessionPackageVersion>

View File

@ -12,7 +12,6 @@ using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing.Metadata;
using Resources = Microsoft.AspNetCore.Mvc.Core.Resources;
namespace Microsoft.AspNetCore.Mvc.Internal

View File

@ -11,7 +11,7 @@ using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing.Metadata;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Options;

View File

@ -3,17 +3,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Matching;
using Microsoft.AspNetCore.Routing.Metadata;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.AspNetCore.Routing.Template;
using Microsoft.Extensions.Primitives;
@ -295,11 +292,16 @@ namespace Microsoft.AspNetCore.Mvc.Internal
var defaults = new RouteValueDictionary(nonInlineDefaults);
EnsureRequiredValuesInDefaults(action.RouteValues, defaults);
var metadataCollection = BuildEndpointMetadata(action, routeName, source, suppressLinkGeneration);
var metadataCollection = BuildEndpointMetadata(
action,
routeName,
new RouteValueDictionary(action.RouteValues),
source,
suppressLinkGeneration);
var endpoint = new MatcherEndpoint(
next => invokerDelegate,
RoutePatternFactory.Parse(template, defaults, constraints: null),
new RouteValueDictionary(action.RouteValues),
order,
metadataCollection,
action.DisplayName);
@ -310,6 +312,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
private static EndpointMetadataCollection BuildEndpointMetadata(
ActionDescriptor action,
string routeName,
RouteValueDictionary requiredValues,
object source,
bool suppressLinkGeneration)
{
@ -323,10 +326,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
metadata.AddRange(action.EndpointMetadata);
}
if (!string.IsNullOrEmpty(routeName))
{
metadata.Add(new RouteNameMetadata(routeName));
}
metadata.Add(new RouteValuesAddressMetadata(routeName, requiredValues));
// Add filter descriptors to endpoint metadata
if (action.FilterDescriptors != null && action.FilterDescriptors.Count > 0)
@ -349,7 +349,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
{
metadata.Add(new HttpMethodMetadata(httpMethodActionConstraint.HttpMethods));
}
else if (actionConstraint is ConsumesAttribute consumesAttribute &&
else if (actionConstraint is ConsumesAttribute consumesAttribute &&
!metadata.OfType<ConsumesMetadata>().Any())
{
metadata.Add(new ConsumesMetadata(consumesAttribute.ContentTypes.ToArray()));
@ -444,16 +444,6 @@ namespace Microsoft.AspNetCore.Mvc.Internal
public List<MvcEndpointInfo> ConventionalEndpointInfos { get; }
private class RouteNameMetadata : IRouteNameMetadata
{
public RouteNameMetadata(string routeName)
{
Name = routeName;
}
public string Name { get; }
}
private class SuppressLinkGenerationMetadata : ISuppressLinkGenerationMetadata { }
}
}

View File

@ -142,7 +142,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
return Task.CompletedTask;
},
RoutePatternFactory.Parse("/"),
new RouteValueDictionary(),
0,
EndpointMetadataCollection.Empty,
Http415EndpointDisplayName);

View File

@ -15,21 +15,16 @@ namespace Microsoft.AspNetCore.Mvc.Routing
{
private readonly ILogger<EndpointRoutingUrlHelper> _logger;
private readonly LinkGenerator _linkGenerator;
private readonly IEndpointFinder<RouteValuesAddress> _routeValuesBasedEndpointFinder;
/// <summary>
/// Initializes a new instance of the <see cref="EndpointRoutingUrlHelper"/> class using the specified
/// <paramref name="actionContext"/>.
/// </summary>
/// <param name="actionContext">The <see cref="Mvc.ActionContext"/> for the current request.</param>
/// <param name="routeValuesBasedEndpointFinder">
/// The <see cref="IEndpointFinder{T}"/> which finds endpoints by required route values.
/// </param>
/// <param name="linkGenerator">The <see cref="LinkGenerator"/> used to generate the link.</param>
/// <param name="logger">The <see cref="ILogger"/>.</param>
public EndpointRoutingUrlHelper(
ActionContext actionContext,
IEndpointFinder<RouteValuesAddress> routeValuesBasedEndpointFinder,
LinkGenerator linkGenerator,
ILogger<EndpointRoutingUrlHelper> logger)
: base(actionContext)
@ -45,7 +40,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
}
_linkGenerator = linkGenerator;
_routeValuesBasedEndpointFinder = routeValuesBasedEndpointFinder;
_logger = logger;
}
@ -85,23 +79,10 @@ namespace Microsoft.AspNetCore.Mvc.Routing
valuesDictionary["controller"] = urlActionContext.Controller;
}
var endpoints = _routeValuesBasedEndpointFinder.FindEndpoints(
new RouteValuesAddress()
{
ExplicitValues = valuesDictionary,
AmbientValues = AmbientValues
});
var successfullyGeneratedLink = _linkGenerator.TryGetLink(
new LinkGeneratorContext
{
HttpContext = ActionContext.HttpContext,
Endpoints = endpoints,
ExplicitValues = valuesDictionary,
AmbientValues = AmbientValues
},
ActionContext.HttpContext,
valuesDictionary,
out var link);
if (!successfullyGeneratedLink)
{
//TODO: log here
@ -122,22 +103,10 @@ namespace Microsoft.AspNetCore.Mvc.Routing
var valuesDictionary = routeContext.Values as RouteValueDictionary ?? GetValuesDictionary(routeContext.Values);
var endpoints = _routeValuesBasedEndpointFinder.FindEndpoints(
new RouteValuesAddress()
{
RouteName = routeContext.RouteName,
ExplicitValues = valuesDictionary,
AmbientValues = AmbientValues
});
var successfullyGeneratedLink = _linkGenerator.TryGetLink(
new LinkGeneratorContext
{
HttpContext = ActionContext.HttpContext,
Endpoints = endpoints,
ExplicitValues = valuesDictionary,
AmbientValues = AmbientValues
},
ActionContext.HttpContext,
routeContext.RouteName,
valuesDictionary,
out var link);
if (!successfullyGeneratedLink)

View File

@ -51,12 +51,10 @@ namespace Microsoft.AspNetCore.Mvc.Routing
{
var services = httpContext.RequestServices;
var linkGenerator = services.GetRequiredService<LinkGenerator>();
var routeValuesBasedEndpointFinder = services.GetRequiredService<IEndpointFinder<RouteValuesAddress>>();
var logger = services.GetRequiredService<ILogger<EndpointRoutingUrlHelper>>();
urlHelper = new EndpointRoutingUrlHelper(
context,
routeValuesBasedEndpointFinder,
linkGenerator,
logger);
}

View File

@ -6,7 +6,7 @@ using System.Linq;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Routing.Metadata;
using Microsoft.AspNetCore.Routing;
namespace Microsoft.AspNetCore.Mvc.Cors.Internal
{

View File

@ -14,7 +14,7 @@ using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing.Metadata;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;

View File

@ -66,7 +66,9 @@ namespace Microsoft.AspNetCore.Mvc.Internal
var endpoint = Assert.Single(endpoints);
var matcherEndpoint = Assert.IsType<MatcherEndpoint>(endpoint);
var endpointValue = matcherEndpoint.RequiredValues["Name"];
var routeValuesAddressMetadata = matcherEndpoint.Metadata.GetMetadata<RouteValuesAddressMetadata>();
Assert.NotNull(routeValuesAddressMetadata);
var endpointValue = routeValuesAddressMetadata.RequiredValues["Name"];
Assert.Equal(routeValue, endpointValue);
Assert.Equal(displayName, matcherEndpoint.DisplayName);
@ -389,7 +391,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
}
[Fact]
public void Endpoints_ConventionalRoute_WithNoRouteName_DoesNotAddRouteNameMetadata()
public void Endpoints_ConventionalRoute_WithEmptyRouteName_CreatesMetadataWithEmptyRouteName()
{
// Arrange
var actionDescriptorCollection = GetActionDescriptorCollection(
@ -404,8 +406,9 @@ namespace Microsoft.AspNetCore.Mvc.Internal
// Assert
var endpoint = Assert.Single(endpoints);
var matcherEndpoint = Assert.IsType<MatcherEndpoint>(endpoint);
var routeNameMetadata = matcherEndpoint.Metadata.GetMetadata<IRouteNameMetadata>();
Assert.Null(routeNameMetadata);
var routeValuesAddressNameMetadata = matcherEndpoint.Metadata.GetMetadata<IRouteValuesAddressMetadata>();
Assert.NotNull(routeValuesAddressNameMetadata);
Assert.Equal(string.Empty, routeValuesAddressNameMetadata.Name);
}
[Fact]
@ -428,17 +431,17 @@ namespace Microsoft.AspNetCore.Mvc.Internal
(ep) =>
{
var matcherEndpoint = Assert.IsType<MatcherEndpoint>(ep);
var routeNameMetadata = matcherEndpoint.Metadata.GetMetadata<IRouteNameMetadata>();
Assert.NotNull(routeNameMetadata);
Assert.Equal("namedRoute", routeNameMetadata.Name);
var routeValuesAddressMetadata = matcherEndpoint.Metadata.GetMetadata<IRouteValuesAddressMetadata>();
Assert.NotNull(routeValuesAddressMetadata);
Assert.Equal("namedRoute", routeValuesAddressMetadata.Name);
Assert.Equal("named/Home/Index/{id?}", matcherEndpoint.RoutePattern.RawText);
},
(ep) =>
{
var matcherEndpoint = Assert.IsType<MatcherEndpoint>(ep);
var routeNameMetadata = matcherEndpoint.Metadata.GetMetadata<IRouteNameMetadata>();
Assert.NotNull(routeNameMetadata);
Assert.Equal("namedRoute", routeNameMetadata.Name);
var routeValuesAddressMetadata = matcherEndpoint.Metadata.GetMetadata<IRouteValuesAddressMetadata>();
Assert.NotNull(routeValuesAddressMetadata);
Assert.Equal("namedRoute", routeValuesAddressMetadata.Name);
Assert.Equal("named/Products/Details/{id?}", matcherEndpoint.RoutePattern.RawText);
});
}

View File

@ -1,15 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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 System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Routing;
@ -379,7 +375,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
return new MatcherEndpoint(
(r) => null,
RoutePatternFactory.Parse("/"),
new RouteValueDictionary(),
0,
new EndpointMetadataCollection(metadata),
$"test: {action?.DisplayName}");

View File

@ -223,7 +223,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
return new MatcherEndpoint(
(next) => null,
RoutePatternFactory.Parse(template),
new RouteValueDictionary(),
0,
new EndpointMetadataCollection(metadata),
$"test: {template} - {string.Join(", ", consumesMetadata?.ContentTypes ?? Array.Empty<string>())}");

View File

@ -56,7 +56,17 @@ namespace Microsoft.AspNetCore.Mvc.Routing
requiredValues: new { controller = "Orders", action = "GetAll" },
routeName: "OrdersApi");
var urlHelper = CreateUrlHelper(new[] { endpoint1, endpoint2 });
urlHelper.ActionContext.RouteData.Values["id"] = "500";
// Set the endpoint feature and current context just as a normal request to MVC app would be
var endpointFeature = new EndpointFeature();
urlHelper.ActionContext.HttpContext.Features.Set<IEndpointFeature>(endpointFeature);
endpointFeature.Endpoint = endpoint1;
endpointFeature.Values = new RouteValueDictionary
{
["controller"] = "Orders",
["action"] = "GetById",
["id"] = "500"
};
// Act
var url = urlHelper.RouteUrl(
@ -132,7 +142,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
endpoints.Add(new MatcherEndpoint(
next => httpContext => Task.CompletedTask,
RoutePatternFactory.Parse(template),
new RouteValueDictionary(),
0,
EndpointMetadataCollection.Empty,
null));
@ -147,7 +156,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
Endpoint = new MatcherEndpoint(
next => cntxt => Task.CompletedTask,
RoutePatternFactory.Parse("/"),
new RouteValueDictionary(),
0,
EndpointMetadataCollection.Empty,
null)
@ -280,17 +288,13 @@ namespace Microsoft.AspNetCore.Mvc.Routing
{
if (metadataCollection == null)
{
metadataCollection = EndpointMetadataCollection.Empty;
if (!string.IsNullOrEmpty(routeName))
{
metadataCollection = new EndpointMetadataCollection(new[] { new RouteNameMetadata(routeName) });
}
metadataCollection = new EndpointMetadataCollection(
new RouteValuesAddressMetadata(routeName, new RouteValueDictionary(requiredValues)));
}
return new MatcherEndpoint(
next => (httpContext) => Task.CompletedTask,
RoutePatternFactory.Parse(template, defaults, constraints: null),
new RouteValueDictionary(requiredValues),
order,
metadataCollection,
null);
@ -316,22 +320,11 @@ namespace Microsoft.AspNetCore.Mvc.Routing
return new MatcherEndpoint(
next => c => Task.CompletedTask,
RoutePatternFactory.Parse(template, defaults, constraints: null),
new RouteValueDictionary(),
0,
EndpointMetadataCollection.Empty,
null);
}
private class RouteNameMetadata : IRouteNameMetadata
{
public RouteNameMetadata(string routeName)
{
Name = routeName;
}
public string Name { get; }
}
private class SuppressLinkGenerationMetadata : ISuppressLinkGenerationMetadata { }
}
}

View File

@ -11,7 +11,7 @@ using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Routing.Metadata;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;