Copy action constraints and EndPointMetadata when setting up a PageActionDescriptor (#8208)

* Copy action constraints and EndPointMetadata when setting up a PageActionDescriptor

Fixes #8207
This commit is contained in:
Pranav K 2018-08-03 12:04:44 -07:00 committed by GitHub
parent ac410b76d9
commit a375cba359
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 1 deletions

View File

@ -75,6 +75,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
var descriptor = new PageActionDescriptor
{
ActionConstraints = selector.ActionConstraints.ToList(),
AreaName = model.AreaName,
AttributeRouteInfo = new AttributeRouteInfo
{
Name = selector.AttributeRouteModel.Name,
@ -84,11 +86,11 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
SuppressPathMatching = selector.AttributeRouteModel.SuppressPathMatching,
},
DisplayName = $"Page: {model.ViewEnginePath}",
EndpointMetadata = selector.EndpointMetadata.ToList(),
FilterDescriptors = Array.Empty<FilterDescriptor>(),
Properties = new Dictionary<object, object>(model.Properties),
RelativePath = model.RelativePath,
ViewEnginePath = model.ViewEnginePath,
AreaName = model.AreaName,
};
foreach (var kvp in model.RouteValues)

View File

@ -49,6 +49,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
ActionConstraints = actionDescriptor.ActionConstraints,
AttributeRouteInfo = actionDescriptor.AttributeRouteInfo,
BoundProperties = boundProperties,
EndpointMetadata = actionDescriptor.EndpointMetadata,
FilterDescriptors = filters,
HandlerMethods = handlerMethods,
HandlerTypeInfo = applicationModel.HandlerType,

View File

@ -4,6 +4,7 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.Extensions.Options;
using Moq;
@ -167,6 +168,74 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
Assert.Equal("Accounts/Test/{id:int?}", descriptor.AttributeRouteInfo.Template);
}
[Fact]
public void GetDescriptors_CopiesActionConstraintsFromModel()
{
// Arrange
var expected = Mock.Of<IActionConstraint>();
var model = new PageRouteModel("/Areas/Accounts/Pages/Test.cshtml", "/Test", "Accounts")
{
Selectors =
{
new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel(),
ActionConstraints = { expected }
}
},
};
var applicationModelProvider = new TestPageRouteModelProvider(model);
var provider = new PageActionDescriptorProvider(
new[] { applicationModelProvider },
GetAccessor<MvcOptions>(),
GetRazorPagesOptions());
var context = new ActionDescriptorProviderContext();
// Act
provider.OnProvidersExecuting(context);
// Assert
var result = Assert.Single(context.Results);
var descriptor = Assert.IsType<PageActionDescriptor>(result);
Assert.Equal(model.RelativePath, descriptor.RelativePath);
var actual = Assert.Single(descriptor.ActionConstraints);
Assert.Same(expected, actual);
}
[Fact]
public void GetDescriptors_CopiesEndPointMetadataFromModel()
{
// Arrange
var expected = new object();
var model = new PageRouteModel("/Test.cshtml", "/Test", "Accounts")
{
Selectors =
{
new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel(),
EndpointMetadata = { expected }
}
},
};
var applicationModelProvider = new TestPageRouteModelProvider(model);
var provider = new PageActionDescriptorProvider(
new[] { applicationModelProvider },
GetAccessor<MvcOptions>(),
GetRazorPagesOptions());
var context = new ActionDescriptorProviderContext();
// Act
provider.OnProvidersExecuting(context);
// Assert
var result = Assert.Single(context.Results);
var descriptor = Assert.IsType<PageActionDescriptor>(result);
Assert.Equal(model.RelativePath, descriptor.RelativePath);
var actual = Assert.Single(descriptor.EndpointMetadata);
Assert.Same(expected, actual);
}
[Fact]
public void GetDescriptors_AddsActionDescriptorForEachSelector()
{

View File

@ -25,6 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{
ActionConstraints = new List<IActionConstraintMetadata>(),
AttributeRouteInfo = new AttributeRouteInfo(),
EndpointMetadata = new List<object>(),
FilterDescriptors = new List<FilterDescriptor>(),
RelativePath = "/Foo",
RouteValues = new Dictionary<string, string>(),
@ -40,6 +41,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
// Assert
Assert.Same(actionDescriptor.ActionConstraints, actual.ActionConstraints);
Assert.Same(actionDescriptor.AttributeRouteInfo, actual.AttributeRouteInfo);
Assert.Same(actionDescriptor.EndpointMetadata, actual.EndpointMetadata);
Assert.Same(actionDescriptor.RelativePath, actual.RelativePath);
Assert.Same(actionDescriptor.RouteValues, actual.RouteValues);
Assert.Same(actionDescriptor.ViewEnginePath, actual.ViewEnginePath);