MonoRail学习笔记十八:在VM中可以使用哪些系统变量
我们知道在vm中可以直接使用Session、SiteRoot等,那么我们还可以使用哪些默认的系统变量呢?
其实可以直接在vm中使用的系统变量都是在NVelocityViewEngine类的CreateContext方法中定义的。下面我们就看看到底定义了哪些(详见代码中的注释说明):
其实可以直接在vm中使用的系统变量都是在NVelocityViewEngine类的CreateContext方法中定义的。下面我们就看看到底定义了哪些(详见代码中的注释说明):
1
private IContext CreateContext(IRailsEngineContext context, Controller controller)
2
{
3
Hashtable innerContext = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
4
//对应的controller中的方法和属性可以直接在vm中使用
5
innerContext.Add(TemplateKeys.Controller, controller);
//当前请求的上下文,请求,Session等可以直接使用
6
innerContext.Add(TemplateKeys.Context, context);
7
innerContext.Add(TemplateKeys.Request, context.Request);
8
innerContext.Add(TemplateKeys.Response, context.Response);
9
innerContext.Add(TemplateKeys.Session, context.Session);
10
//对应的controller中的Resources中所有资源可以直接在vm中使用
11
if (controller.Resources != null)
12
{
13
foreach(String key in controller.Resources.Keys)
14
{
15
innerContext[key] = controller.Resources[key];
16
}
17
}
18
//所有Params中的值可以直接在vm中使用
19
foreach(String key in context.Params.AllKeys)
20
{
21
if (key == null) continue; // Nasty bug?
22
object value = context.Params[key];
23
if (value == null) continue;
24
innerContext[key] = value;
25
}
26
27
// list from : http://msdn2.microsoft.com/en-us/library/hfa3fa08.aspx
28
object[] builtInHelpers =
29
new object[]
30
{
31
new StaticAccessorHelper<Byte>(),
32
new StaticAccessorHelper<SByte>(),
33
new StaticAccessorHelper<Int16>(),
34
new StaticAccessorHelper<Int32>(),
35
new StaticAccessorHelper<Int64>(),
36
new StaticAccessorHelper<UInt16>(),
37
new StaticAccessorHelper<UInt32>(),
38
new StaticAccessorHelper<UInt64>(),
39
new StaticAccessorHelper<Single>(),
40
new StaticAccessorHelper<Double>(),
41
new StaticAccessorHelper<Boolean>(),
42
new StaticAccessorHelper<Char>(),
43
new StaticAccessorHelper<Decimal>(),
44
new StaticAccessorHelper<String>(),
45
new StaticAccessorHelper<Guid>(),
46
new StaticAccessorHelper<DateTime>()
47
};
48
//常见的系统类可以之间使用,比如可以直接在vm中使用$Int32.MaxValue
49
foreach(object helper in builtInHelpers)
50
{
51
innerContext[helper.GetType().GetGenericArguments()[0].Name] = helper;
52
}
53
//定义的Helper类可以直接在vm中使用
54
if (controller.Helpers != null)
55
{
56
foreach (object key in controller.Helpers.Keys)
57
{
58
innerContext[key] = controller.Helpers[key];
59
}
60
}
61
62
// Adding flash as a collection and each individual item
63
//定义的Flash值可以直接使用
64
if (context.Flash != null)
65
{
66
innerContext[Flash.FlashKey] = context.Flash;
67
68
foreach(DictionaryEntry entry in context.Flash)
69
{
70
if (entry.Value == null) continue;
71
innerContext[entry.Key] = entry.Value;
72
}
73
}
74
//定义的PropertyBag值可以直接使用
75
if (controller.PropertyBag != null)
76
{
77
foreach(DictionaryEntry entry in controller.PropertyBag)
78
{
79
if (entry.Value == null) continue;
80
innerContext[entry.Key] = entry.Value;
81
}
82
}
83
//SiteRoot可以直接使用 ---取得的是应用程序的路径
84
innerContext[TemplateKeys.SiteRoot] = context.ApplicationPath;
85
86
return new VelocityContext(innerContext);
87
}

2

3

4

5

//当前请求的上下文,请求,Session等可以直接使用
6

7

8

9

10

//对应的controller中的Resources中所有资源可以直接在vm中使用
11

12

13

14

15

16

17

18

//所有Params中的值可以直接在vm中使用
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

//常见的系统类可以之间使用,比如可以直接在vm中使用$Int32.MaxValue
49

50

51

52

53

//定义的Helper类可以直接在vm中使用
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

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?