This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
RazorPageActivator.cs
87 lines (77 loc) · 3.5 KB
/
RazorPageActivator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Reflection;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace Microsoft.AspNetCore.Mvc.Razor
{
/// <inheritdoc />
public class RazorPageActivator : IRazorPageActivator
{
// Name of the "public TModel Model" property on RazorPage<TModel>
private const string ModelPropertyName = "Model";
private readonly ConcurrentDictionary<Type, RazorPagePropertyActivator> _activationInfo;
private readonly IModelMetadataProvider _metadataProvider;
// Value accessors for common singleton properties activated in a RazorPage.
private readonly RazorPagePropertyActivator.PropertyValueAccessors _propertyAccessors;
/// <summary>
/// Initializes a new instance of the <see cref="RazorPageActivator"/> class.
/// </summary>
public RazorPageActivator(
IModelMetadataProvider metadataProvider,
IUrlHelperFactory urlHelperFactory,
IJsonHelper jsonHelper,
DiagnosticSource diagnosticSource,
HtmlEncoder htmlEncoder,
IModelExpressionProvider modelExpressionProvider)
{
_activationInfo = new ConcurrentDictionary<Type, RazorPagePropertyActivator>();
_metadataProvider = metadataProvider;
_propertyAccessors = new RazorPagePropertyActivator.PropertyValueAccessors
{
UrlHelperAccessor = context => urlHelperFactory.GetUrlHelper(context),
JsonHelperAccessor = context => jsonHelper,
DiagnosticSourceAccessor = context => diagnosticSource,
HtmlEncoderAccessor = context => htmlEncoder,
ModelExpressionProviderAccessor = context => modelExpressionProvider,
};
}
/// <inheritdoc />
public void Activate(IRazorPage page, ViewContext context)
{
if (page == null)
{
throw new ArgumentNullException(nameof(page));
}
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var pageType = page.GetType();
RazorPagePropertyActivator propertyActivator;
if (!_activationInfo.TryGetValue(pageType, out propertyActivator))
{
// Look for a property named "Model". If it is non-null, we'll assume this is
// the equivalent of TModel Model property on RazorPage<TModel>.
//
// Otherwise if we don't have a model property the activator will just skip setting
// the view data.
var modelType = pageType.GetRuntimeProperty(ModelPropertyName)?.PropertyType;
propertyActivator = new RazorPagePropertyActivator(
pageType,
modelType,
_metadataProvider,
_propertyAccessors);
propertyActivator = _activationInfo.GetOrAdd(pageType, propertyActivator);
}
propertyActivator.Activate(page, context);
}
}
}