Remove model type from RazorPageAttribute
This commit is contained in:
parent
20c04d099e
commit
628dfc39c1
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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).
|
||||||
|
|
|
||||||
|
|
@ -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),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue