Skip to content

Commit 5ac1a64

Browse files
committed
Centralized view model creation
The code for creating view models is largely redundant. Centralized the pattern into the new `CreateErrorViewModel` method.
1 parent fac29f2 commit 5ac1a64

1 file changed

Lines changed: 35 additions & 21 deletions

File tree

Ignia.Topics.Web.Mvc/Controllers/ErrorControllerBase{T}.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ public virtual ActionResult Error(string title = "General Error") {
4141
/*------------------------------------------------------------------------------------------------------------------------
4242
| Instantiate view model
4343
\-----------------------------------------------------------------------------------------------------------------------*/
44-
var viewModel = new T();
45-
viewModel.Key = "Error";
46-
viewModel.WebPath = "/Error/Error";
47-
viewModel.ContentType = "Page";
48-
viewModel.Title = title;
49-
viewModel.MetaKeywords = "";
50-
viewModel.MetaDescription = "";
44+
var viewModel = CreateErrorViewModel("Error", title);
5145

5246
/*------------------------------------------------------------------------------------------------------------------------
5347
| Return the view
@@ -73,13 +67,7 @@ public virtual ActionResult NotFound(string title = "Page Not Found") {
7367
/*------------------------------------------------------------------------------------------------------------------------
7468
| Instantiate view model
7569
\-----------------------------------------------------------------------------------------------------------------------*/
76-
var viewModel = new T();
77-
viewModel.Key = "NotFound";
78-
viewModel.WebPath = "/Error/NotFound";
79-
viewModel.ContentType = "Page";
80-
viewModel.Title = title;
81-
viewModel.MetaKeywords = "";
82-
viewModel.MetaDescription = "";
70+
var viewModel = CreateErrorViewModel("NotFound", title);
8371

8472
/*------------------------------------------------------------------------------------------------------------------------
8573
| Return the view
@@ -105,13 +93,7 @@ public virtual ActionResult InternalServer(string title = "Internal Server Error
10593
/*------------------------------------------------------------------------------------------------------------------------
10694
| Instantiate model
10795
\-----------------------------------------------------------------------------------------------------------------------*/
108-
var viewModel = new T();
109-
viewModel.Key = "InternalServer";
110-
viewModel.WebPath = "/Error/InternalServer";
111-
viewModel.ContentType = "Page";
112-
viewModel.Title = title;
113-
viewModel.MetaKeywords = "";
114-
viewModel.MetaDescription = "";
96+
var viewModel = CreateErrorViewModel("InternalServer", title);
11597

11698
/*------------------------------------------------------------------------------------------------------------------------
11799
| Return the view
@@ -120,6 +102,38 @@ public virtual ActionResult InternalServer(string title = "Internal Server Error
120102

121103
}
122104

105+
/*==========================================================================================================================
106+
| CREATE ERROR VIEW MODEL
107+
\-------------------------------------------------------------------------------------------------------------------------*/
108+
/// <summary>
109+
/// Establishes an empty view model, populates it with appropriate values based on the method parameters, and returns it
110+
/// for use in one of the actions.
111+
/// </summary>
112+
/// <param name="key">
113+
/// The key name to use for the <see cref="IPageTopicViewModelCore"/>'s <c>Key</c> and <c>WebPath</c> properties.
114+
/// </param>
115+
/// <param name="title">The title of the error page.</param>
116+
/// <returns>A view model representing an error message.</returns>
117+
public virtual T CreateErrorViewModel(string key, string title) {
118+
119+
/*------------------------------------------------------------------------------------------------------------------------
120+
| Instantiate model
121+
\-----------------------------------------------------------------------------------------------------------------------*/
122+
var viewModel = new T();
123+
viewModel.Key = key;
124+
viewModel.WebPath = "/Error/" + key;
125+
viewModel.ContentType = "Page";
126+
viewModel.Title = title;
127+
viewModel.MetaKeywords = "";
128+
viewModel.MetaDescription = "";
129+
130+
/*------------------------------------------------------------------------------------------------------------------------
131+
| Return the view
132+
\-----------------------------------------------------------------------------------------------------------------------*/
133+
return viewModel;
134+
135+
}
136+
123137
} // Class
124138

125139
} // Namespace

0 commit comments

Comments
 (0)