New test.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / WebFormView.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.Globalization;\r
16     using System.IO;\r
17     using System.Web.Mvc.Resources;\r
18 \r
19     public class WebFormView : IView {\r
20 \r
21         private IBuildManager _buildManager;\r
22 \r
23         public WebFormView(string viewPath)\r
24             : this(viewPath, null) {\r
25         }\r
26 \r
27         public WebFormView(string viewPath, string masterPath) {\r
28             if (String.IsNullOrEmpty(viewPath)) {\r
29                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "viewPath");\r
30             }\r
31 \r
32             ViewPath = viewPath;\r
33             MasterPath = masterPath ?? String.Empty;\r
34         }\r
35 \r
36         internal IBuildManager BuildManager {\r
37             get {\r
38                 if (_buildManager == null) {\r
39                     _buildManager = new BuildManagerWrapper();\r
40                 }\r
41                 return _buildManager;\r
42             }\r
43             set {\r
44                 _buildManager = value;\r
45             }\r
46         }\r
47 \r
48         public string MasterPath {\r
49             get;\r
50             private set;\r
51         }\r
52 \r
53         public string ViewPath {\r
54             get;\r
55             private set;\r
56         }\r
57 \r
58         public virtual void Render(ViewContext viewContext, TextWriter writer) {\r
59             if (viewContext == null) {\r
60                 throw new ArgumentNullException("viewContext");\r
61             }\r
62 \r
63             object viewInstance = BuildManager.CreateInstanceFromVirtualPath(ViewPath, typeof(object));\r
64             if (viewInstance == null) {\r
65                 throw new InvalidOperationException(\r
66                     String.Format(\r
67                         CultureInfo.CurrentUICulture,\r
68                         MvcResources.WebFormViewEngine_ViewCouldNotBeCreated,\r
69                         ViewPath));\r
70             }\r
71 \r
72             ViewPage viewPage = viewInstance as ViewPage;\r
73             if (viewPage != null) {\r
74                 RenderViewPage(viewContext, viewPage);\r
75                 return;\r
76             }\r
77 \r
78             ViewUserControl viewUserControl = viewInstance as ViewUserControl;\r
79             if (viewUserControl != null) {\r
80                 RenderViewUserControl(viewContext, viewUserControl);\r
81                 return;\r
82             }\r
83 \r
84             throw new InvalidOperationException(\r
85                 String.Format(\r
86                     CultureInfo.CurrentUICulture,\r
87                     MvcResources.WebFormViewEngine_WrongViewBase,\r
88                     ViewPath));\r
89         }\r
90 \r
91         private void RenderViewPage(ViewContext context, ViewPage page) {\r
92             if (!String.IsNullOrEmpty(MasterPath)) {\r
93                 page.MasterLocation = MasterPath;\r
94             }\r
95 \r
96             page.ViewData = context.ViewData;\r
97             page.RenderView(context);\r
98         }\r
99 \r
100         private void RenderViewUserControl(ViewContext context, ViewUserControl control) {\r
101             if (!String.IsNullOrEmpty(MasterPath)) {\r
102                 throw new InvalidOperationException(MvcResources.WebFormViewEngine_UserControlCannotHaveMaster);\r
103             }\r
104 \r
105             control.ViewData = context.ViewData;\r
106             control.RenderView(context);\r
107         }\r
108     }\r
109 }\r