Merge pull request #8653 from aspnet/jamesnk/master

Merge release/2.2
This commit is contained in:
James Newton-King 2018-10-27 12:31:37 +13:00 committed by GitHub
commit 1a093686e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 8 deletions

View File

@ -618,6 +618,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
model.DisplayName = action.DisplayName; model.DisplayName = action.DisplayName;
// REVIEW: When should conventions be run
// Metadata should have lower precedence that data source metadata
if (conventions != null) if (conventions != null)
{ {
foreach (var convention in conventions) foreach (var convention in conventions)
@ -638,8 +640,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
bool suppressLinkGeneration, bool suppressLinkGeneration,
bool suppressPathMatching) bool suppressPathMatching)
{ {
metadata.Add(action); // Add action metadata first so it has a low precedence
if (action.EndpointMetadata != null) if (action.EndpointMetadata != null)
{ {
foreach (var d in action.EndpointMetadata) foreach (var d in action.EndpointMetadata)
@ -647,6 +648,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
metadata.Add(d); metadata.Add(d);
} }
} }
metadata.Add(action);
if (dataTokens != null) if (dataTokens != null)
{ {

View File

@ -1305,6 +1305,36 @@ namespace Microsoft.AspNetCore.Mvc.Internal
}); });
} }
[Fact]
public void Endpoints_AttributeRoutes_ActionMetadataDoesNotOverrideDataSourceMetadata()
{
// Arrange
var actionDescriptorCollection = GetActionDescriptorCollection(
CreateActionDescriptor(new { controller = "TestController", action = "TestAction" },
"{controller}/{action}/{id?}",
new List<object> { new RouteValuesAddressMetadata("fakeroutename", new RouteValueDictionary(new { fake = "Fake!" })) })
);
var dataSource = CreateMvcEndpointDataSource(actionDescriptorCollection);
// Act
var endpoints = dataSource.Endpoints;
// Assert
Assert.Collection(
endpoints,
(ep) =>
{
var matcherEndpoint = Assert.IsType<RouteEndpoint>(ep);
Assert.Equal("TestController/TestAction/{id?}", matcherEndpoint.RoutePattern.RawText);
Assert.Equal(0, matcherEndpoint.Order);
var routeValuesAddress = matcherEndpoint.Metadata.GetMetadata<IRouteValuesAddressMetadata>();
Assert.Equal("{controller}/{action}/{id?}", routeValuesAddress.RouteName);
Assert.Equal("TestController", routeValuesAddress.RequiredValues["controller"]);
Assert.Equal("TestAction", routeValuesAddress.RequiredValues["action"]);
});
}
private MvcEndpointDataSource CreateMvcEndpointDataSource( private MvcEndpointDataSource CreateMvcEndpointDataSource(
IActionDescriptorCollectionProvider actionDescriptorCollectionProvider = null, IActionDescriptorCollectionProvider actionDescriptorCollectionProvider = null,
MvcEndpointInvokerFactory mvcEndpointInvokerFactory = null) MvcEndpointInvokerFactory mvcEndpointInvokerFactory = null)
@ -1387,6 +1417,11 @@ namespace Microsoft.AspNetCore.Mvc.Internal
actionDescriptors.Add(CreateActionDescriptor(requiredValue, attributeRouteTemplate)); actionDescriptors.Add(CreateActionDescriptor(requiredValue, attributeRouteTemplate));
} }
return GetActionDescriptorCollection(actionDescriptors.ToArray());
}
private IActionDescriptorCollectionProvider GetActionDescriptorCollection(params ActionDescriptor[] actionDescriptors)
{
var actionDescriptorCollectionProviderMock = new Mock<IActionDescriptorCollectionProvider>(); var actionDescriptorCollectionProviderMock = new Mock<IActionDescriptorCollectionProvider>();
actionDescriptorCollectionProviderMock actionDescriptorCollectionProviderMock
.Setup(m => m.ActionDescriptors) .Setup(m => m.ActionDescriptors)
@ -1394,12 +1429,10 @@ namespace Microsoft.AspNetCore.Mvc.Internal
return actionDescriptorCollectionProviderMock.Object; return actionDescriptorCollectionProviderMock.Object;
} }
private ActionDescriptor CreateActionDescriptor(string controller, string action, string area = null) private ActionDescriptor CreateActionDescriptor(
{ object requiredValues,
return CreateActionDescriptor(new { controller = controller, action = action, area = area }, attributeRouteTemplate: null); string attributeRouteTemplate = null,
} IList<object> metadata = null)
private ActionDescriptor CreateActionDescriptor(object requiredValues, string attributeRouteTemplate = null)
{ {
var actionDescriptor = new ActionDescriptor(); var actionDescriptor = new ActionDescriptor();
var routeValues = new RouteValueDictionary(requiredValues); var routeValues = new RouteValueDictionary(requiredValues);
@ -1415,6 +1448,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
Template = attributeRouteTemplate Template = attributeRouteTemplate
}; };
} }
actionDescriptor.EndpointMetadata = metadata;
return actionDescriptor; return actionDescriptor;
} }