-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTopicViewResult.cs
103 lines (90 loc) · 5.81 KB
/
TopicViewResult.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using OnTopic.Metadata;
namespace OnTopic.AspNetCore.Mvc {
/*============================================================================================================================
| CLASS: TOPIC VIEW RESULT
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Provides a custom version of <see cref="ViewResult" /> capable of determining the most appropriate view.
/// </summary>
public class TopicViewResult : ViewResult {
/*==========================================================================================================================
| CONSTRUCTOR
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Constructs a new instances of a <see cref="TopicViewResult"/> based on a supplied <paramref name="contentType"/> and,
/// optionally, <paramref name="view"/>.
/// </summary>
/// <remarks>
/// If the <paramref name="contentType"/> is not provided, it is assumed to be <c>Page</c>. If the <paramref name="view"/>
/// is not provided, it is assumed to be <paramref name="contentType"/>.
/// </remarks>
public TopicViewResult(
ViewDataDictionary viewData,
ITempDataDictionary? tempData,
object viewModel,
string? contentType = null,
string? view = null
) : base() {
/*------------------------------------------------------------------------------------------------------------------------
| Validate parameters
\-----------------------------------------------------------------------------------------------------------------------*/
Contract.Requires(viewData, nameof(viewData));
Contract.Requires(viewModel, nameof(viewModel));
/*------------------------------------------------------------------------------------------------------------------------
| Set local variables
\-----------------------------------------------------------------------------------------------------------------------*/
ViewData = viewData;
TempData = tempData;
ViewData.Model = viewModel;
TopicContentType = contentType?? TopicContentType;
TopicView = view ?? ContentType;
//ViewName = TopicView;
}
/*==========================================================================================================================
| PROPERTY: TOPIC CONTENT TYPE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Represents the <see cref="ContentTypeDescriptor"/> name associated with the current <see cref="Topic"/>.
/// </summary>
/// <remarks>
/// The preferred nomenclature for the name of a <see cref="ContentTypeDescriptor"/> is simply <c>ContentType</c>. The
/// base <see cref="ViewResult"/> class has an existing <see cref="ViewResult.ContentType"/> property representing the
/// HTTP response value, however. As such, <see cref="TopicContentType"/> is used to disambiguate the terms.
/// </remarks>
public string TopicContentType { get; } = "Page";
/*==========================================================================================================================
| PROPERTY: TOPIC VIEW
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Represents the <see cref="Topic.View"/> property. This may be overwritten later by a variety of sources.
/// </summary>
/// <remarks>
/// The associated <see cref="TopicViewResultExecutor"/> will fall back to the <see cref="TopicView"/> if the view isn't
/// set via other sources, such as the HTTP <c>accepts</c> header, the query string, etc.
/// </remarks>
public string TopicView { get; }
/*==========================================================================================================================
| METHOD: EXECUTE RESULT (ASYNC)
\-------------------------------------------------------------------------------------------------------------------------*/
/// <inheritdoc/>
public override async Task ExecuteResultAsync(ActionContext context) {
/*------------------------------------------------------------------------------------------------------------------------
| Validate parameters
\-----------------------------------------------------------------------------------------------------------------------*/
Contract.Requires(context, nameof(context));
/*------------------------------------------------------------------------------------------------------------------------
| Call associated executor
\-----------------------------------------------------------------------------------------------------------------------*/
var executor = context.HttpContext.RequestServices.GetRequiredService<IActionResultExecutor<TopicViewResult>>();
await executor.ExecuteAsync(context, this).ConfigureAwait(false);
}
} //Class
} //Namespace