summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.WebPages/WebPageContext.cs
blob: 13118a561ea8f7b398fd61a2443c5dec4b3c4eaf (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
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Web.WebPages.Html;

namespace System.Web.WebPages
{
    // Class for containing various pieces of data required by a WebPage
    public class WebPageContext
    {
        private static readonly object _sourceFileKey = new object();
        private Stack<TextWriter> _outputStack;
        private Stack<Dictionary<string, SectionWriter>> _sectionWritersStack;
        private IDictionary<object, dynamic> _pageData;
        private ValidationHelper _validation;
        private ModelStateDictionary _modelStateDictionary;

        public WebPageContext()
            : this(context: null, page: null, model: null)
        {
        }

        public WebPageContext(HttpContextBase context, WebPageRenderingBase page, object model)
        {
            HttpContext = context;
            Page = page;
            Model = model;
        }

        public static WebPageContext Current
        {
            get
            {
                // The TemplateStack stores instances of WebPageRenderingBase. 
                // Retrieve the top-most item from the stack and cast it to WebPageBase. 
                var httpContext = Web.HttpContext.Current;
                if (httpContext != null)
                {
                    var contextWrapper = new HttpContextWrapper(httpContext);
                    var currentTemplate = TemplateStack.GetCurrentTemplate(contextWrapper);
                    var currentPage = (currentTemplate as WebPageRenderingBase);

                    return (currentPage == null) ? null : currentPage.PageContext;
                }
                return null;
            }
        }

        internal HttpContextBase HttpContext { get; set; }

        public object Model { get; internal set; }

        internal ModelStateDictionary ModelState
        {
            get
            {
                if (_modelStateDictionary == null)
                {
                    _modelStateDictionary = new ModelStateDictionary();
                }
                return _modelStateDictionary;
            }
        }

        internal ValidationHelper Validation
        {
            get
            {
                if (_validation == null)
                {
                    Debug.Assert(HttpContext != null, "HttpContext must be initalized for Validation to work.");
                    _validation = new ValidationHelper(HttpContext, ModelState);
                }
                return _validation;
            }
            private set { _validation = value; }
        }

        internal Action<TextWriter> BodyAction { get; set; }

        internal Stack<TextWriter> OutputStack
        {
            get
            {
                if (_outputStack == null)
                {
                    _outputStack = new Stack<TextWriter>();
                }
                return _outputStack;
            }
            set { _outputStack = value; }
        }

        public WebPageRenderingBase Page { get; internal set; }

        public IDictionary<object, dynamic> PageData
        {
            get
            {
                if (_pageData == null)
                {
                    _pageData = new PageDataDictionary<dynamic>();
                }
                return _pageData;
            }
            internal set { _pageData = value; }
        }

        internal Stack<Dictionary<string, SectionWriter>> SectionWritersStack
        {
            get
            {
                if (_sectionWritersStack == null)
                {
                    _sectionWritersStack = new Stack<Dictionary<string, SectionWriter>>();
                }
                return _sectionWritersStack;
            }
            set { _sectionWritersStack = value; }
        }

        // NOTE: We use a hashset because order doesn't matter and we want to eliminate duplicates
        internal HashSet<string> SourceFiles
        {
            get
            {
                HashSet<string> sourceFiles = HttpContext.Items[_sourceFileKey] as HashSet<string>;
                if (sourceFiles == null)
                {
                    sourceFiles = new HashSet<string>();
                    HttpContext.Items[_sourceFileKey] = sourceFiles;
                }
                return sourceFiles;
            }
        }

        internal static WebPageContext CreateNestedPageContext<TModel>(WebPageContext parentContext, IDictionary<object, dynamic> pageData, TModel model, bool isLayoutPage)
        {
            var nestedContext = new WebPageContext
            {
                HttpContext = parentContext.HttpContext,
                OutputStack = parentContext.OutputStack,
                Validation = parentContext.Validation,
                PageData = pageData,
                Model = model,
            };
            if (isLayoutPage)
            {
                nestedContext.BodyAction = parentContext.BodyAction;
                nestedContext.SectionWritersStack = parentContext.SectionWritersStack;
            }
            return nestedContext;
        }
    }
}