2009-05-18 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / ViewResultBase.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Diagnostics.CodeAnalysis;\r
16 \r
17     public abstract class ViewResultBase : ActionResult {\r
18         private TempDataDictionary _tempData;\r
19         private ViewDataDictionary _viewData;\r
20         private ViewEngineCollection _viewEngineCollection;\r
21         private string _viewName;\r
22 \r
23         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
24             Justification = "This entire type is meant to be mutable.")]\r
25         public TempDataDictionary TempData {\r
26             get {\r
27                 if (_tempData == null) {\r
28                     _tempData = new TempDataDictionary();\r
29                 }\r
30                 return _tempData;\r
31             }\r
32             set {\r
33                 _tempData = value;\r
34             }\r
35         }\r
36 \r
37         public IView View {\r
38             get;\r
39             set;\r
40         }\r
41 \r
42         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
43             Justification = "This entire type is meant to be mutable.")]\r
44         public ViewDataDictionary ViewData {\r
45             get {\r
46                 if (_viewData == null) {\r
47                     _viewData = new ViewDataDictionary();\r
48                 }\r
49                 return _viewData;\r
50             }\r
51             set {\r
52                 _viewData = value;\r
53             }\r
54         }\r
55 \r
56         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
57             Justification = "This entire type is meant to be mutable.")]\r
58         public ViewEngineCollection ViewEngineCollection {\r
59             get {\r
60                 return _viewEngineCollection ?? ViewEngines.Engines;\r
61             }\r
62             set {\r
63                 _viewEngineCollection = value;\r
64             }\r
65         }\r
66 \r
67         public string ViewName {\r
68             get {\r
69                 return _viewName ?? String.Empty;\r
70             }\r
71             set {\r
72                 _viewName = value;\r
73             }\r
74         }\r
75 \r
76         public override void ExecuteResult(ControllerContext context) {\r
77             if (context == null) {\r
78                 throw new ArgumentNullException("context");\r
79             }\r
80             if (String.IsNullOrEmpty(ViewName)) {\r
81                 ViewName = context.RouteData.GetRequiredString("action");\r
82             }\r
83 \r
84             ViewEngineResult result = null;\r
85 \r
86             if (View == null) {\r
87                 result = FindView(context);\r
88                 View = result.View;\r
89             }\r
90 \r
91             ViewContext viewContext = new ViewContext(context, View, ViewData, TempData);\r
92             View.Render(viewContext, context.HttpContext.Response.Output);\r
93 \r
94             if (result != null) {\r
95                 result.ViewEngine.ReleaseView(context, View);\r
96             }\r
97         }\r
98 \r
99         protected abstract ViewEngineResult FindView(ControllerContext context);\r
100     }\r
101 }\r