2010-06-21 Marek Habersack <mhabersack@novell.com>
[mcs.git] / class / System.Web.Mvc / System.Web.Mvc / WebFormView.cs
blob7167d8c75aab4e0a7f87e57d07a741525d9d7681
1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * This software is subject to the Microsoft Public License (Ms-PL).
6 * A copy of the license can be found in the license.htm file included
7 * in this distribution.
9 * You must not remove this notice, or any other, from this software.
11 * ***************************************************************************/
13 namespace System.Web.Mvc {
14 using System;
15 using System.Globalization;
16 using System.IO;
17 using System.Web.Mvc.Resources;
19 public class WebFormView : IView {
21 private IBuildManager _buildManager;
23 public WebFormView(string viewPath)
24 : this(viewPath, null) {
27 public WebFormView(string viewPath, string masterPath) {
28 if (String.IsNullOrEmpty(viewPath)) {
29 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "viewPath");
32 ViewPath = viewPath;
33 MasterPath = masterPath ?? String.Empty;
36 internal IBuildManager BuildManager {
37 get {
38 if (_buildManager == null) {
39 _buildManager = new BuildManagerWrapper();
41 return _buildManager;
43 set {
44 _buildManager = value;
48 public string MasterPath {
49 get;
50 private set;
53 public string ViewPath {
54 get;
55 private set;
58 public virtual void Render(ViewContext viewContext, TextWriter writer) {
59 if (viewContext == null) {
60 throw new ArgumentNullException("viewContext");
63 object viewInstance = BuildManager.CreateInstanceFromVirtualPath(ViewPath, typeof(object));
64 if (viewInstance == null) {
65 throw new InvalidOperationException(
66 String.Format(
67 CultureInfo.CurrentUICulture,
68 MvcResources.WebFormViewEngine_ViewCouldNotBeCreated,
69 ViewPath));
72 ViewPage viewPage = viewInstance as ViewPage;
73 if (viewPage != null) {
74 RenderViewPage(viewContext, viewPage);
75 return;
78 ViewUserControl viewUserControl = viewInstance as ViewUserControl;
79 if (viewUserControl != null) {
80 RenderViewUserControl(viewContext, viewUserControl);
81 return;
84 throw new InvalidOperationException(
85 String.Format(
86 CultureInfo.CurrentUICulture,
87 MvcResources.WebFormViewEngine_WrongViewBase,
88 ViewPath));
91 private void RenderViewPage(ViewContext context, ViewPage page) {
92 if (!String.IsNullOrEmpty(MasterPath)) {
93 page.MasterLocation = MasterPath;
96 page.ViewData = context.ViewData;
97 page.RenderView(context);
100 private void RenderViewUserControl(ViewContext context, ViewUserControl control) {
101 if (!String.IsNullOrEmpty(MasterPath)) {
102 throw new InvalidOperationException(MvcResources.WebFormViewEngine_UserControlCannotHaveMaster);
105 control.ViewData = context.ViewData;
106 control.RenderView(context);