This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DefaultViewLocationConventions.cs
145 lines (117 loc) · 6.49 KB
/
DefaultViewLocationConventions.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
namespace Nancy.Conventions
{
using System;
using System.Collections.Generic;
using Nancy.ViewEngines;
/// <summary>
/// Defines the default static contents conventions.
/// </summary>
public class DefaultViewLocationConventions : IConvention
{
/// <summary>
/// Initialise any conventions this class "owns".
/// </summary>
/// <param name="conventions">Convention object instance.</param>
public void Initialise(NancyConventions conventions)
{
ConfigureViewLocationConventions(conventions);
}
/// <summary>
/// Asserts that the conventions that this class "owns" are valid.
/// </summary>
/// <param name="conventions">Conventions object instance.</param>
/// <returns>Tuple containing true/false for valid/invalid, and any error messages.</returns>
public Tuple<bool, string> Validate(NancyConventions conventions)
{
if (conventions.ViewLocationConventions == null)
{
return Tuple.Create(false, "The view conventions cannot be null.");
}
return (conventions.ViewLocationConventions.Count > 0) ?
Tuple.Create(true, string.Empty) :
Tuple.Create(false, "The view conventions cannot be empty.");
}
private static void ConfigureViewLocationConventions(NancyConventions conventions)
{
conventions.ViewLocationConventions = new List<Func<string, object, ViewLocationContext, string>>
{
(viewName, model, viewLocationContext) =>{
if (string.IsNullOrEmpty(viewLocationContext.ModulePath))
{
return string.Empty;
}
var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' });
return string.Concat("views/", path, "/", viewLocationContext.ModuleName, "/", viewName, "-", viewLocationContext.Context.Culture);
},
// 0 Handles: views / *modulepath* / *modulename* / *viewname*
(viewName, model, viewLocationContext) =>{
if (string.IsNullOrEmpty(viewLocationContext.ModulePath))
{
return string.Empty;
}
var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' });
return string.Concat("views/", path, "/", viewLocationContext.ModuleName, "/", viewName);
},
(viewName, model, viewLocationContext) =>{
if (string.IsNullOrEmpty(viewLocationContext.ModulePath))
{
return string.Empty;
}
var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' });
return string.Concat(path, "/", viewLocationContext.ModuleName, "/", viewName, "-", viewLocationContext.Context.Culture);
},
// 1 Handles: *modulepath* / *modulename* / *viewname*
(viewName, model, viewLocationContext) =>{
if (string.IsNullOrEmpty(viewLocationContext.ModulePath))
{
return string.Empty;
}
var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' });
return string.Concat(path, "/", viewLocationContext.ModuleName, "/", viewName);
},
(viewName, model, viewLocationContext) =>{
return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat("views/", viewLocationContext.ModulePath.TrimStart(new[] { '/' }), "/", viewName, "-", viewLocationContext.Context.Culture);
},
// 2 Handles: views / *modulepath* / *viewname*
(viewName, model, viewLocationContext) =>{
return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat("views/", viewLocationContext.ModulePath.TrimStart(new[] {'/'}), "/", viewName);
},
(viewName, model, viewLocationContext) =>{
return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat(viewLocationContext.ModulePath.TrimStart(new[] { '/' }), "/", viewName, "-", viewLocationContext.Context.Culture);
},
// 3 Handles: *modulepath* / *viewname*
(viewName, model, viewLocationContext) =>{
return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat(viewLocationContext.ModulePath.TrimStart(new[] { '/' }), "/", viewName);
},
(viewName, model, viewLocationContext) => {
return string.Concat("views/", viewLocationContext.ModuleName, "/", viewName, "-", viewLocationContext.Context.Culture);
},
// 4 Handles: views / *modulename* / *viewname*
(viewName, model, viewLocationContext) => {
return string.Concat("views/", viewLocationContext.ModuleName, "/", viewName);
},
(viewName, model, viewLocationContext) => {
return string.Concat(viewLocationContext.ModuleName, "/", viewName, "-", viewLocationContext.Context.Culture);
},
// 5 Handles: *modulename* / *viewname*
(viewName, model, viewLocationContext) => {
return string.Concat(viewLocationContext.ModuleName, "/", viewName);
},
(viewName, model, viewLocationContext) => {
return string.Concat("views/", viewName, "-", viewLocationContext.Context.Culture);
},
// 6 Handles: views / *viewname*
(viewName, model, viewLocationContext) => {
return string.Concat("views/", viewName);
},
(viewName, model, viewLocationContext) => {
return string.Concat(viewName, "-", viewLocationContext.Context.Culture);
},
// 7 Handles: *viewname*
(viewName, model, viewLocationContext) => {
return viewName;
}
};
}
}
}