From 628dfc39c1678f49b1f0721a6dc9e98f396c30e5 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 5 Jun 2017 16:07:14 -0700 Subject: [PATCH] Remove model type from RazorPageAttribute --- .../ApplicationParts/CompiledPageFeatureProvider.cs | 2 -- .../Infrastructure/RazorPageAttribute.cs | 5 +---- .../Internal/DefaultPageLoader.cs | 10 ++++------ .../CompiledPageApplicationModelProviderTest.cs | 2 +- .../Internal/DefaultPageLoaderTest.cs | 10 +++++----- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageFeatureProvider.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageFeatureProvider.cs index 03e0fcf22a..e8e72a90c8 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageFeatureProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationParts/CompiledPageFeatureProvider.cs @@ -53,12 +53,10 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts foreach (var page in manifests.SelectMany(m => m.CompiledPages)) { var normalizedPath = ViewPath.NormalizePath(page.Path); - var modelType = page.CompiledType.GetProperty("Model")?.PropertyType; var pageAttribute = new RazorPageAttribute( normalizedPath, page.CompiledType, - modelType, page.RoutePrefix); var viewDescriptor = new CompiledViewDescriptor diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/RazorPageAttribute.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/RazorPageAttribute.cs index 8ec3a6ab57..900827a018 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/RazorPageAttribute.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/RazorPageAttribute.cs @@ -8,15 +8,12 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure { public class RazorPageAttribute : RazorViewAttribute { - public RazorPageAttribute(string path, Type viewType, Type modelType, string routeTemplate) + public RazorPageAttribute(string path, Type viewType, string routeTemplate) : base(path, viewType) { - ModelType = modelType; RouteTemplate = routeTemplate; } - public Type ModelType { get; } - public string RouteTemplate { get; } } } diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/DefaultPageLoader.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/DefaultPageLoader.cs index 930d2ade69..b58c000905 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/DefaultPageLoader.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/DefaultPageLoader.cs @@ -30,14 +30,9 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal var viewDescriptor = compileTask.GetAwaiter().GetResult(); var viewAttribute = viewDescriptor.ViewAttribute; - // Pages always have a model type. If it's not set explicitly by the developer using - // @model, it will be the same as the page type. - var modelType = viewAttribute.ViewType.GetProperty(ModelPropertyName)?.PropertyType; - var pageAttribute = new RazorPageAttribute( viewAttribute.Path, viewAttribute.ViewType, - modelType, routeTemplate: null); return CreateDescriptor(actionDescriptor, pageAttribute); @@ -49,7 +44,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal RazorPageAttribute pageAttribute) { var pageType = pageAttribute.ViewType.GetTypeInfo(); - var modelType = pageAttribute.ModelType?.GetTypeInfo(); + + // Pages always have a model type. If it's not set explicitly by the developer using + // @model, it will be the same as the page type. + var modelType = pageAttribute.ViewType.GetProperty(ModelPropertyName)?.PropertyType?.GetTypeInfo(); // Now we want to find the handler methods. If the model defines any handlers, then we'll use those, // otherwise look at the page itself (unless the page IS the model, in which case we already looked). diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/CompiledPageApplicationModelProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/CompiledPageApplicationModelProviderTest.cs index 4119f572cd..3f40cbe5f1 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/CompiledPageApplicationModelProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/CompiledPageApplicationModelProviderTest.cs @@ -139,7 +139,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal return new CompiledViewDescriptor { RelativePath = path, - ViewAttribute = new RazorPageAttribute(path, typeof(object), typeof(object), prefix), + ViewAttribute = new RazorPageAttribute(path, typeof(object), prefix), }; } diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/DefaultPageLoaderTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/DefaultPageLoaderTest.cs index 65e5755b8a..b3a603221c 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/DefaultPageLoaderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/Internal/DefaultPageLoaderTest.cs @@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Act var actual = DefaultPageLoader.CreateDescriptor(expected, - new RazorPageAttribute(expected.RelativePath, typeof(EmptyPage), null, "")); + new RazorPageAttribute(expected.RelativePath, typeof(EmptyPage), "")); // Assert Assert.Same(expected.ActionConstraints, actual.ActionConstraints); @@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Act var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(), - new RazorPageAttribute("/Pages/Index", type, type, "")); + new RazorPageAttribute("/Pages/Index", type, "")); // Assert Assert.Empty(result.BoundProperties); @@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Act var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(), - new RazorPageAttribute("/Pages/Index", type, typeof(EmptyPageModel), "")); + new RazorPageAttribute("/Pages/Index", type, "")); // Assert Assert.Empty(result.BoundProperties); @@ -138,7 +138,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Act var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(), - new RazorPageAttribute("/Pages/Index", type, typeof(ModelWithHandler), "")); + new RazorPageAttribute("/Pages/Index", type, "")); // Assert Assert.Collection(result.BoundProperties, p => Assert.Equal("BindMe", p.Name)); @@ -175,7 +175,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal // Act var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(), - new RazorPageAttribute("/Pages/Index", type, typeof(PocoModel), "")); + new RazorPageAttribute("/Pages/Index", type, "")); // Assert Assert.Collection(result.BoundProperties, p => Assert.Equal("BindMe", p.Name));