New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / 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     using System.IO;\r
17 \r
18     public abstract class ViewResultBase : ActionResult {\r
19         private TempDataDictionary _tempData;\r
20         private ViewDataDictionary _viewData;\r
21         private ViewEngineCollection _viewEngineCollection;\r
22         private string _viewName;\r
23 \r
24         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
25             Justification = "This entire type is meant to be mutable.")]\r
26         public TempDataDictionary TempData {\r
27             get {\r
28                 if (_tempData == null) {\r
29                     _tempData = new TempDataDictionary();\r
30                 }\r
31                 return _tempData;\r
32             }\r
33             set {\r
34                 _tempData = value;\r
35             }\r
36         }\r
37 \r
38         public IView View {\r
39             get;\r
40             set;\r
41         }\r
42 \r
43         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
44             Justification = "This entire type is meant to be mutable.")]\r
45         public ViewDataDictionary ViewData {\r
46             get {\r
47                 if (_viewData == null) {\r
48                     _viewData = new ViewDataDictionary();\r
49                 }\r
50                 return _viewData;\r
51             }\r
52             set {\r
53                 _viewData = value;\r
54             }\r
55         }\r
56 \r
57         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
58             Justification = "This entire type is meant to be mutable.")]\r
59         public ViewEngineCollection ViewEngineCollection {\r
60             get {\r
61                 return _viewEngineCollection ?? ViewEngines.Engines;\r
62             }\r
63             set {\r
64                 _viewEngineCollection = value;\r
65             }\r
66         }\r
67 \r
68         public string ViewName {\r
69             get {\r
70                 return _viewName ?? String.Empty;\r
71             }\r
72             set {\r
73                 _viewName = value;\r
74             }\r
75         }\r
76 \r
77         public override void ExecuteResult(ControllerContext context) {\r
78             if (context == null) {\r
79                 throw new ArgumentNullException("context");\r
80             }\r
81             if (String.IsNullOrEmpty(ViewName)) {\r
82                 ViewName = context.RouteData.GetRequiredString("action");\r
83             }\r
84 \r
85             ViewEngineResult result = null;\r
86 \r
87             if (View == null) {\r
88                 result = FindView(context);\r
89                 View = result.View;\r
90             }\r
91 \r
92             TextWriter writer = context.HttpContext.Response.Output;\r
93             ViewContext viewContext = new ViewContext(context, View, ViewData, TempData, writer);\r
94             View.Render(viewContext, writer);\r
95 \r
96             if (result != null) {\r
97                 result.ViewEngine.ReleaseView(context, View);\r
98             }\r
99         }\r
100 \r
101         protected abstract ViewEngineResult FindView(ControllerContext context);\r
102     }\r
103 }\r