Remove model type from RazorPageAttribute

This commit is contained in:
Pranav K 2017-06-05 16:07:14 -07:00
parent 20c04d099e
commit 628dfc39c1
5 changed files with 11 additions and 18 deletions

View File

@ -53,12 +53,10 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
foreach (var page in manifests.SelectMany(m => m.CompiledPages)) foreach (var page in manifests.SelectMany(m => m.CompiledPages))
{ {
var normalizedPath = ViewPath.NormalizePath(page.Path); var normalizedPath = ViewPath.NormalizePath(page.Path);
var modelType = page.CompiledType.GetProperty("Model")?.PropertyType;
var pageAttribute = new RazorPageAttribute( var pageAttribute = new RazorPageAttribute(
normalizedPath, normalizedPath,
page.CompiledType, page.CompiledType,
modelType,
page.RoutePrefix); page.RoutePrefix);
var viewDescriptor = new CompiledViewDescriptor var viewDescriptor = new CompiledViewDescriptor

View File

@ -8,15 +8,12 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{ {
public class RazorPageAttribute : RazorViewAttribute 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) : base(path, viewType)
{ {
ModelType = modelType;
RouteTemplate = routeTemplate; RouteTemplate = routeTemplate;
} }
public Type ModelType { get; }
public string RouteTemplate { get; } public string RouteTemplate { get; }
} }
} }

View File

@ -30,14 +30,9 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var viewDescriptor = compileTask.GetAwaiter().GetResult(); var viewDescriptor = compileTask.GetAwaiter().GetResult();
var viewAttribute = viewDescriptor.ViewAttribute; 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( var pageAttribute = new RazorPageAttribute(
viewAttribute.Path, viewAttribute.Path,
viewAttribute.ViewType, viewAttribute.ViewType,
modelType,
routeTemplate: null); routeTemplate: null);
return CreateDescriptor(actionDescriptor, pageAttribute); return CreateDescriptor(actionDescriptor, pageAttribute);
@ -49,7 +44,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
RazorPageAttribute pageAttribute) RazorPageAttribute pageAttribute)
{ {
var pageType = pageAttribute.ViewType.GetTypeInfo(); 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, // 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). // otherwise look at the page itself (unless the page IS the model, in which case we already looked).

View File

@ -139,7 +139,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
return new CompiledViewDescriptor return new CompiledViewDescriptor
{ {
RelativePath = path, RelativePath = path,
ViewAttribute = new RazorPageAttribute(path, typeof(object), typeof(object), prefix), ViewAttribute = new RazorPageAttribute(path, typeof(object), prefix),
}; };
} }

View File

@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
// Act // Act
var actual = DefaultPageLoader.CreateDescriptor(expected, var actual = DefaultPageLoader.CreateDescriptor(expected,
new RazorPageAttribute(expected.RelativePath, typeof(EmptyPage), null, "")); new RazorPageAttribute(expected.RelativePath, typeof(EmptyPage), ""));
// Assert // Assert
Assert.Same(expected.ActionConstraints, actual.ActionConstraints); Assert.Same(expected.ActionConstraints, actual.ActionConstraints);
@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
// Act // Act
var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(), var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(),
new RazorPageAttribute("/Pages/Index", type, type, "")); new RazorPageAttribute("/Pages/Index", type, ""));
// Assert // Assert
Assert.Empty(result.BoundProperties); Assert.Empty(result.BoundProperties);
@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
// Act // Act
var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(), var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(),
new RazorPageAttribute("/Pages/Index", type, typeof(EmptyPageModel), "")); new RazorPageAttribute("/Pages/Index", type, ""));
// Assert // Assert
Assert.Empty(result.BoundProperties); Assert.Empty(result.BoundProperties);
@ -138,7 +138,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
// Act // Act
var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(), var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(),
new RazorPageAttribute("/Pages/Index", type, typeof(ModelWithHandler), "")); new RazorPageAttribute("/Pages/Index", type, ""));
// Assert // Assert
Assert.Collection(result.BoundProperties, p => Assert.Equal("BindMe", p.Name)); Assert.Collection(result.BoundProperties, p => Assert.Equal("BindMe", p.Name));
@ -175,7 +175,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
// Act // Act
var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(), var result = DefaultPageLoader.CreateDescriptor(new PageActionDescriptor(),
new RazorPageAttribute("/Pages/Index", type, typeof(PocoModel), "")); new RazorPageAttribute("/Pages/Index", type, ""));
// Assert // Assert
Assert.Collection(result.BoundProperties, p => Assert.Equal("BindMe", p.Name)); Assert.Collection(result.BoundProperties, p => Assert.Equal("BindMe", p.Name));