summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.WebPages/StartPage.cs
blob: 125ac47ff5b243b39158268f838791bf0905bd61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using Microsoft.Internal.Web.Utils;

namespace System.Web.WebPages
{
    /// <summary>
    /// Wrapper class to be used by _pagestart.cshtml files to call into
    /// the actual page.
    /// Most of the properties and methods just delegate the call to ChildPage.XXX
    /// </summary>
    public abstract class StartPage : WebPageRenderingBase
    {
        public WebPageRenderingBase ChildPage { get; set; }

        public override HttpContextBase Context
        {
            get { return ChildPage.Context; }
            set { ChildPage.Context = value; }
        }

        public override string Layout
        {
            get { return ChildPage.Layout; }
            set
            {
                if (value == null)
                {
                    ChildPage.Layout = null;
                }
                else
                {
                    ChildPage.Layout = NormalizeLayoutPagePath(value);
                }
            }
        }

        public override IDictionary<object, dynamic> PageData
        {
            get { return ChildPage.PageData; }
        }

        public override dynamic Page
        {
            get { return ChildPage.Page; }
        }

        internal bool RunPageCalled { get; set; }

        public override void ExecutePageHierarchy()
        {
            // Push the current pagestart on the stack. 
            TemplateStack.Push(Context, this);
            try
            {
                // Execute the developer-written code of the InitPage
                Execute();

                // If the child page wasn't explicitly run by the developer of the InitPage, then run it now.
                // The child page is either the next InitPage, or the final WebPage.
                if (!RunPageCalled)
                {
                    RunPage();
                }
            }
            finally
            {
                TemplateStack.Pop(Context);
            }
        }

        /// <summary>
        /// Returns either the root-most init page, or the provided page itself if no init page is found
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
            Justification = "Start Pages are instances of WebPageRenderingBase. It might be possible to have WebPageExecuting bases that are not in the same inheritance tree as StartPages")]
        public static WebPageRenderingBase GetStartPage(WebPageRenderingBase page, string fileName, IEnumerable<string> supportedExtensions)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }
            if (String.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, CommonResources.Argument_Cannot_Be_Null_Or_Empty, "fileName"), "fileName");
            }
            if (supportedExtensions == null)
            {
                throw new ArgumentNullException("supportedExtensions");
            }

            // Use the page's VirtualPathFactory if available
            return GetStartPage(page, page.VirtualPathFactory ?? VirtualPathFactoryManager.Instance,
                                HttpRuntime.AppDomainAppVirtualPath, fileName, supportedExtensions);
        }

        internal static WebPageRenderingBase GetStartPage(WebPageRenderingBase page, IVirtualPathFactory virtualPathFactory, string appDomainAppVirtualPath,
                                                          string fileName, IEnumerable<string> supportedExtensions)
        {
            // Build up a list of pages to execute, such as one of the following:
            // ~/somepage.cshtml
            // ~/_pageStart.cshtml --> ~/somepage.cshtml
            // ~/_pageStart.cshtml --> ~/sub/_pageStart.cshtml --> ~/sub/somepage.cshtml
            WebPageRenderingBase currentPage = page;
            var pageDirectory = VirtualPathUtility.GetDirectory(page.VirtualPath);

            // Start with the requested page's directory, find the init page,
            // and then traverse up the hierarchy to find init pages all the
            // way up to the root of the app.
            while (!String.IsNullOrEmpty(pageDirectory) && pageDirectory != "/" && PathUtil.IsWithinAppRoot(appDomainAppVirtualPath, pageDirectory))
            {
                // Go through the list of supported extensions
                foreach (var extension in supportedExtensions)
                {
                    var virtualPath = VirtualPathUtility.Combine(pageDirectory, fileName + "." + extension);

                    // Can we build a file from the current path?
                    if (virtualPathFactory.Exists(virtualPath))
                    {
                        var parentStartPage = virtualPathFactory.CreateInstance<StartPage>(virtualPath);
                        parentStartPage.VirtualPath = virtualPath;
                        parentStartPage.ChildPage = currentPage;
                        parentStartPage.VirtualPathFactory = virtualPathFactory;
                        currentPage = parentStartPage;

                        break;
                    }
                }

                pageDirectory = currentPage.GetDirectory(pageDirectory);
            }

            // At this point 'currentPage' is the root-most StartPage (if there were
            // any StartPages at all) or it is the requested page itself.
            return currentPage;
        }

        public override HelperResult RenderPage(string path, params object[] data)
        {
            return ChildPage.RenderPage(NormalizePath(path), data);
        }

        public void RunPage()
        {
            RunPageCalled = true;
            //ChildPage.PageContext = PageContext;
            ChildPage.ExecutePageHierarchy();
        }

        public override void Write(HelperResult result)
        {
            ChildPage.Write(result);
        }

        public override void WriteLiteral(object value)
        {
            ChildPage.WriteLiteral(value);
        }

        public override void Write(object value)
        {
            ChildPage.Write(value);
        }

        protected internal override TextWriter GetOutputWriter()
        {
            return ChildPage.GetOutputWriter();
        }
    }
}