forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpCodePage.cs
More file actions
112 lines (92 loc) · 3.78 KB
/
SharpCodePage.cs
File metadata and controls
112 lines (92 loc) · 3.78 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace ServiceStack.Script
{
public abstract class SharpCodePage : IDisposable
{
public string VirtualPath { get; set; }
public string Layout { get; set; }
public SharpPage LayoutPage { get; set; }
public PageFormat Format { get; set; }
public Dictionary<string, object> Args { get; } = new Dictionary<string, object>();
public ScriptContext Context { get; set; }
public ISharpPages Pages { get; set; }
public ScriptScopeContext Scope { get; set; }
private MethodInfo renderMethod;
private MethodInvoker renderInvoker;
protected SharpCodePage(string layout = null)
{
Layout = layout;
}
public async Task WriteAsync(ScriptScopeContext scope)
{
var renderParams = renderMethod.GetParameters();
var args = new object[renderParams.Length];
Dictionary<string, string> requestParams = null;
for (var i = 0; i < renderParams.Length; i++)
{
var renderParam = renderParams[i];
var arg = scope.GetValue(renderParam.Name);
if (arg == null)
{
if (requestParams == null)
requestParams = (scope.GetValue("Request") as Web.IRequest)?.GetRequestParams();
if (requestParams != null && requestParams.TryGetValue(renderParam.Name, out var reqParam))
arg = reqParam;
}
args[i] = arg;
}
try
{
var result = renderInvoker(this, args);
if (result != null)
{
var str = result.ToString();
await scope.OutputStream.WriteAsync(str);
}
}
catch (Exception ex)
{
throw new TargetInvocationException($"Failed to invoke render method on '{GetType().Name}': {ex.Message}", ex);
}
}
public bool HasInit { get; private set; }
public virtual SharpCodePage Init()
{
if (!HasInit)
{
HasInit = true;
var type = GetType();
if (Format == null)
Format = Context.PageFormats.First();
var pageAttr = type.FirstAttribute<PageAttribute>();
VirtualPath = pageAttr.VirtualPath;
if (Layout == null)
Layout = pageAttr?.Layout;
LayoutPage = Pages.ResolveLayoutPage(this, Layout);
var pageArgs = type.AllAttributes<PageArgAttribute>();
foreach (var pageArg in pageArgs)
{
Args[pageArg.Name] = pageArg.Value;
}
if (!Context.CodePageInvokers.TryGetValue(type, out var tuple))
{
var method = type.GetInstanceMethods().FirstOrDefault(x => x.Name.EndsWithIgnoreCase("render"));
if (method == null)
throw new NotSupportedException($"Template Code Page '{GetType().Name}' does not have a 'render' method");
var invoker = TypeExtensions.GetInvokerToCache(method);
Context.CodePageInvokers[type] = tuple = Tuple.Create(method, invoker);
}
renderMethod = tuple.Item1;
renderInvoker = tuple.Item2;
}
return this;
}
public virtual void Dispose()
{
}
}
}