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))
{
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

View File

@ -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; }
}
}

View File

@ -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).

View File

@ -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),
};
}

View File

@ -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));