MonoRail学习笔记十八:在VM中可以使用哪些系统变量

来源:互联网 发布:中国碳交易 数据 编辑:程序博客网 时间:2024/04/30 09:33

 我们知道在vm中可以直接使用SessionSiteRoot等,那么我们还可以使用哪些默认的系统变量呢?

其实可以直接在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 == nullcontinue// Nasty bug?
22                object value = context.Params[key];
23                if (value == nullcontinue;
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 == nullcontinue;
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 == nullcontinue;
80                    innerContext[entry.Key] = entry.Value;
81                }

82            }

83            //SiteRoot可以直接使用 ---取得的是应用程序的路径
84            innerContext[TemplateKeys.SiteRoot] = context.ApplicationPath;
85
86            return new VelocityContext(innerContext);
87        }

原创粉丝点击