~ubuntu-branches/ubuntu/lucid/mono/lucid

« back to all changes in this revision

Viewing changes to mcs/class/System.Web.Mvc/System.Web.Mvc/WebFormView.cs

  • Committer: Bazaar Package Importer
  • Author(s): Mirco Bauer
  • Date: 2009-07-30 19:35:10 UTC
  • mto: (5.2.2 squeeze)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20090730193510-cdttfvqokq2kmdvh
Tags: upstream-2.4.2.3+dfsg
Import upstream version 2.4.2.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ****************************************************************************
 
2
 *
 
3
 * Copyright (c) Microsoft Corporation. All rights reserved.
 
4
 *
 
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.
 
8
 *
 
9
 * You must not remove this notice, or any other, from this software.
 
10
 *
 
11
 * ***************************************************************************/
 
12
 
 
13
namespace System.Web.Mvc {
 
14
    using System;
 
15
    using System.Globalization;
 
16
    using System.IO;
 
17
    using System.Web.Mvc.Resources;
 
18
 
 
19
    public class WebFormView : IView {
 
20
 
 
21
        private IBuildManager _buildManager;
 
22
 
 
23
        public WebFormView(string viewPath)
 
24
            : this(viewPath, null) {
 
25
        }
 
26
 
 
27
        public WebFormView(string viewPath, string masterPath) {
 
28
            if (String.IsNullOrEmpty(viewPath)) {
 
29
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "viewPath");
 
30
            }
 
31
 
 
32
            ViewPath = viewPath;
 
33
            MasterPath = masterPath ?? String.Empty;
 
34
        }
 
35
 
 
36
        internal IBuildManager BuildManager {
 
37
            get {
 
38
                if (_buildManager == null) {
 
39
                    _buildManager = new BuildManagerWrapper();
 
40
                }
 
41
                return _buildManager;
 
42
            }
 
43
            set {
 
44
                _buildManager = value;
 
45
            }
 
46
        }
 
47
 
 
48
        public string MasterPath {
 
49
            get;
 
50
            private set;
 
51
        }
 
52
 
 
53
        public string ViewPath {
 
54
            get;
 
55
            private set;
 
56
        }
 
57
 
 
58
        public virtual void Render(ViewContext viewContext, TextWriter writer) {
 
59
            if (viewContext == null) {
 
60
                throw new ArgumentNullException("viewContext");
 
61
            }
 
62
 
 
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));
 
70
            }
 
71
 
 
72
            ViewPage viewPage = viewInstance as ViewPage;
 
73
            if (viewPage != null) {
 
74
                RenderViewPage(viewContext, viewPage);
 
75
                return;
 
76
            }
 
77
 
 
78
            ViewUserControl viewUserControl = viewInstance as ViewUserControl;
 
79
            if (viewUserControl != null) {
 
80
                RenderViewUserControl(viewContext, viewUserControl);
 
81
                return;
 
82
            }
 
83
 
 
84
            throw new InvalidOperationException(
 
85
                String.Format(
 
86
                    CultureInfo.CurrentUICulture,
 
87
                    MvcResources.WebFormViewEngine_WrongViewBase,
 
88
                    ViewPath));
 
89
        }
 
90
 
 
91
        private void RenderViewPage(ViewContext context, ViewPage page) {
 
92
            if (!String.IsNullOrEmpty(MasterPath)) {
 
93
                page.MasterLocation = MasterPath;
 
94
            }
 
95
 
 
96
            page.ViewData = context.ViewData;
 
97
            page.RenderView(context);
 
98
        }
 
99
 
 
100
        private void RenderViewUserControl(ViewContext context, ViewUserControl control) {
 
101
            if (!String.IsNullOrEmpty(MasterPath)) {
 
102
                throw new InvalidOperationException(MvcResources.WebFormViewEngine_UserControlCannotHaveMaster);
 
103
            }
 
104
 
 
105
            control.ViewData = context.ViewData;
 
106
            control.RenderView(context);
 
107
        }
 
108
    }
 
109
}