Merge branch 'release/2.1' into dev

This commit is contained in:
Pranav K 2018-03-27 12:29:36 -07:00
commit a72b56f49e
No known key found for this signature in database
GPG Key ID: 1963DA6D96C3057A
3 changed files with 88 additions and 17 deletions

View File

@ -36,18 +36,10 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
foreach (var item in provider.CompiledItems)
{
var descriptor = GetCompiledViewDescriptor(item);
var descriptor = new CompiledViewDescriptor(item, attribute: null);
feature.ViewDescriptors.Add(descriptor);
}
}
}
private static CompiledViewDescriptor GetCompiledViewDescriptor(RazorCompiledItem item)
{
var itemAssembly = item.Type.Assembly;
var razorViewAttribute = itemAssembly.GetCustomAttributes<RazorViewAttribute>()
.FirstOrDefault(attribute => attribute.ViewType == item.Type);
return new CompiledViewDescriptor(item, razorViewAttribute);
}
}
}

View File

@ -7,8 +7,10 @@ using System.Linq;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.AspNetCore.Razor.Hosting;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@ -96,18 +98,19 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
continue;
}
var pageAttribute = (RazorPageAttribute)viewDescriptor.ViewAttribute;
var relativePath = viewDescriptor.RelativePath;
var routeTemplate = GetRouteTemplate(viewDescriptor);
PageRouteModel routeModel = null;
// When RootDirectory and AreaRootDirectory overlap (e.g. RootDirectory = '/', AreaRootDirectory = '/Areas'), we
// only want to allow a page to be associated with the area route.
if (_pagesOptions.AllowAreas && viewDescriptor.RelativePath.StartsWith(areaRootDirectory, StringComparison.OrdinalIgnoreCase))
if (_pagesOptions.AllowAreas && relativePath.StartsWith(areaRootDirectory, StringComparison.OrdinalIgnoreCase))
{
routeModel = _routeModelFactory.CreateAreaRouteModel(viewDescriptor.RelativePath, pageAttribute.RouteTemplate);
routeModel = _routeModelFactory.CreateAreaRouteModel(relativePath, routeTemplate);
}
else if (viewDescriptor.RelativePath.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase))
else if (relativePath.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase))
{
routeModel = _routeModelFactory.CreateRouteModel(pageAttribute.Path, pageAttribute.RouteTemplate);
routeModel = _routeModelFactory.CreateRouteModel(relativePath, routeTemplate);
}
if (routeModel != null)
@ -116,5 +119,23 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
}
}
}
internal static string GetRouteTemplate(CompiledViewDescriptor viewDescriptor)
{
if (viewDescriptor.ViewAttribute != null)
{
return ((RazorPageAttribute)viewDescriptor.ViewAttribute).RouteTemplate;
}
if (viewDescriptor.Item != null)
{
return viewDescriptor.Item.Metadata
.OfType<RazorCompiledItemMetadataAttribute>()
.FirstOrDefault(f => f.Key == RazorPageDocumentClassifierPass.RouteTemplateKey)
?.Value;
}
return null;
}
}
}

View File

@ -82,7 +82,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var descriptors = new[]
{
CreateVersion_2_1_Descriptor("/Pages/About.cshtml"),
CreateVersion_2_1_Descriptor("/Pages/Home.cshtml", "some-prefix"),
CreateVersion_2_1_Descriptor("/Pages/Home.cshtml", metadata: new[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", "some-prefix"),
}),
};
var provider = CreateProvider(descriptors: descriptors);
@ -515,6 +518,63 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
});
}
[Fact]
public void GetRouteTemplate_ReturnsPathFromRazorPageAttribute()
{
// Arrange
var expected = "test";
var descriptor = CreateVersion_2_0_Descriptor("/Pages/Home.cshtml", expected);
// Act
var result = CompiledPageRouteModelProvider.GetRouteTemplate(descriptor);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void GetRouteTemplate_ReturnsNull_IfPageAttributeDoesNotHaveTemplate()
{
// Arrange
var descriptor = CreateVersion_2_0_Descriptor("/Pages/Home.cshtml", routeTemplate: null);
// Act
var result = CompiledPageRouteModelProvider.GetRouteTemplate(descriptor);
// Assert
Assert.Null(result);
}
[Fact]
public void GetRouteTemplate_ReturnsPathFromMetadataAttribute()
{
// Arrange
var expected = "test";
var descriptor = CreateVersion_2_1_Descriptor("/Pages/About.cshtml", metadata: new object[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", expected),
});
// Act
var result = CompiledPageRouteModelProvider.GetRouteTemplate(descriptor);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void GetRouteTemplate_ReturnsNull_IfAttributeDoesNotExist()
{
// Arrange
var descriptor = CreateVersion_2_1_Descriptor("/Pages/About.cshtml");
// Act
var result = CompiledPageRouteModelProvider.GetRouteTemplate(descriptor);
// Assert
Assert.Null(result);
}
private TestCompiledPageRouteModelProvider CreateProvider(
RazorPagesOptions options = null,
IList<CompiledViewDescriptor> descriptors = null,
@ -549,13 +609,11 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
private static CompiledViewDescriptor CreateVersion_2_1_Descriptor(
string path,
string routeTemplate = "",
object[] metadata = null)
{
return new CompiledViewDescriptor
{
RelativePath = path,
ViewAttribute = new RazorPageAttribute(path, typeof(object), routeTemplate),
Item = new TestRazorCompiledItem(typeof(object), "mvc.1.0.razor-page", path, metadata ?? Array.Empty<object>()),
};
}