Merge pull request #569 from knocte/fix_cairo_profile_versions_problem
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / ViewPage.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.Web;\r
17     using System.Web.UI;\r
18 \r
19     [FileLevelControlBuilder(typeof(ViewPageControlBuilder))]\r
20     public class ViewPage : Page, IViewDataContainer {\r
21 \r
22         private string _masterLocation;\r
23         private ViewDataDictionary _viewData;\r
24 \r
25         public AjaxHelper Ajax {\r
26             get;\r
27             set;\r
28         }\r
29 \r
30         public HtmlHelper Html {\r
31             get;\r
32             set;\r
33         }\r
34 \r
35         public string MasterLocation {\r
36             get {\r
37                 return _masterLocation ?? String.Empty;\r
38             }\r
39             set {\r
40                 _masterLocation = value;\r
41             }\r
42         }\r
43 \r
44         public object Model {\r
45             get {\r
46                 return ViewData.Model;\r
47             }\r
48         }\r
49 \r
50         public TempDataDictionary TempData {\r
51             get {\r
52                 return ViewContext.TempData;\r
53             }\r
54         }\r
55 \r
56         public UrlHelper Url {\r
57             get;\r
58             set;\r
59         }\r
60 \r
61         public ViewContext ViewContext {\r
62             get;\r
63             set;\r
64         }\r
65 \r
66         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
67             Justification = "This is the mechanism by which the ViewPage gets its ViewDataDictionary object.")]\r
68         public ViewDataDictionary ViewData {\r
69             get {\r
70                 if (_viewData == null) {\r
71                     SetViewData(new ViewDataDictionary());\r
72                 }\r
73                 return _viewData;\r
74             }\r
75             set {\r
76                 SetViewData(value);\r
77             }\r
78         }\r
79 \r
80         public HtmlTextWriter Writer {\r
81             get;\r
82             private set;\r
83         }\r
84 \r
85         public virtual void InitHelpers() {\r
86             Ajax = new AjaxHelper(ViewContext, this);\r
87             Html = new HtmlHelper(ViewContext, this);\r
88             Url = new UrlHelper(ViewContext.RequestContext);\r
89         }\r
90 \r
91         [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]\r
92         protected override void OnPreInit(EventArgs e) {\r
93             base.OnPreInit(e);\r
94 \r
95             if (!String.IsNullOrEmpty(MasterLocation)) {\r
96                 MasterPageFile = MasterLocation;\r
97             }\r
98         }\r
99 \r
100         protected override void Render(HtmlTextWriter writer) {\r
101             Writer = writer;\r
102             try {\r
103                 base.Render(writer);\r
104             }\r
105             finally {\r
106                 Writer = null;\r
107             }\r
108         }\r
109 \r
110         public virtual void RenderView(ViewContext viewContext) {\r
111             ViewContext = viewContext;\r
112             InitHelpers();\r
113             // Tracing requires Page IDs to be unique.\r
114             ID = Guid.NewGuid().ToString();\r
115             ProcessRequest(HttpContext.Current);\r
116         }\r
117 \r
118         protected virtual void SetViewData(ViewDataDictionary viewData) {\r
119             _viewData = viewData;\r
120         }\r
121     }\r
122 }\r