summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.WebPages/HelperPage.cs
blob: 27b3a5b26340cf73260d7d2b049ccd4b690faf1f (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// 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.IO;
using System.Security.Principal;
using System.Web.Caching;
using System.Web.WebPages.Html;
using System.Web.WebPages.Instrumentation;

namespace System.Web.WebPages
{
    [SuppressMessage("Microsoft.Design", "CA1053:StaticHolderTypesShouldNotHaveConstructors", Justification = "Even though this is essentially a static class, we need helper classes to inherit from it to get their static methods")]
    public class HelperPage
    {
        private static WebPageContext _pageContext;
        private static InstrumentationService _instrumentationService = null;

        private static InstrumentationService InstrumentationService
        {
            get
            {
                if (_instrumentationService == null)
                {
                    _instrumentationService = new InstrumentationService();
                }
                return _instrumentationService;
            }
        }

        public static HttpContextBase Context
        {
            get { return new HttpContextWrapper(HttpContext.Current); }
        }

        public static WebPageRenderingBase CurrentPage
        {
            get { return PageContext.Page; }
        }

        public static dynamic Page
        {
            get { return CurrentPage.Page; }
        }

        public static dynamic Model
        {
            get
            {
                WebPage currentWebPage = CurrentPage as WebPage;
                if (currentWebPage == null)
                {
                    return null;
                }
                return currentWebPage.Model;
            }
        }

        public static ModelStateDictionary ModelState
        {
            get
            {
                WebPage currentWebPage = CurrentPage as WebPage;
                if (currentWebPage == null)
                {
                    return null;
                }
                return currentWebPage.ModelState;
            }
        }

        public static HtmlHelper Html
        {
            get
            {
                WebPage currentWebPage = CurrentPage as WebPage;
                if (currentWebPage == null)
                {
                    return null;
                }
                return currentWebPage.Html;
            }
        }

        public static WebPageContext PageContext
        {
            get { return _pageContext ?? WebPageContext.Current; }
            set { _pageContext = value; }
        }

        public static HttpApplicationStateBase AppState
        {
            get
            {
                if (Context != null)
                {
                    return Context.Application;
                }
                return null;
            }
        }

        public static dynamic App
        {
            get { return CurrentPage.App; }
        }

        public static string VirtualPath
        {
            get { return PageContext.Page.VirtualPath; }
        }

        public static Cache Cache
        {
            get
            {
                if (Context != null)
                {
                    return Context.Cache;
                }
                return null;
            }
        }

        public static HttpRequestBase Request
        {
            get
            {
                if (Context != null)
                {
                    return Context.Request;
                }
                return null;
            }
        }

        public static HttpResponseBase Response
        {
            get
            {
                if (Context != null)
                {
                    return Context.Response;
                }
                return null;
            }
        }

        public static HttpServerUtilityBase Server
        {
            get
            {
                if (Context != null)
                {
                    return Context.Server;
                }
                return null;
            }
        }

        public static HttpSessionStateBase Session
        {
            get
            {
                if (Context != null)
                {
                    return Context.Session;
                }
                return null;
            }
        }

        public static IList<string> UrlData
        {
            get { return CurrentPage.UrlData; }
        }

        public static IPrincipal User
        {
            get { return CurrentPage.User; }
        }

        public static bool IsPost
        {
            get { return CurrentPage.IsPost; }
        }

        public static bool IsAjax
        {
            get { return CurrentPage.IsAjax; }
        }

        public static IDictionary<object, dynamic> PageData
        {
            get { return PageContext.PageData; }
        }

        protected static string HelperVirtualPath { get; set; }

        public static string Href(string path, params object[] pathParts)
        {
            return CurrentPage.Href(path, pathParts);
        }

        public static void WriteTo(TextWriter writer, object value)
        {
            WebPageBase.WriteTo(writer, value);
        }

        public static void WriteLiteralTo(TextWriter writer, object value)
        {
            WebPageBase.WriteLiteralTo(writer, value);
        }

        public static void WriteTo(TextWriter writer, HelperResult value)
        {
            WebPageBase.WriteTo(writer, value);
        }

        public static void WriteLiteralTo(TextWriter writer, HelperResult value)
        {
            WebPageBase.WriteLiteralTo(writer, value);
        }

        public static void WriteAttributeTo(TextWriter writer, string name, PositionTagged<string> prefix, PositionTagged<string> suffix, params AttributeValue[] values)
        {
            CurrentPage.WriteAttributeTo(VirtualPath, writer, name, prefix, suffix, values);
        }

        public static void BeginContext(string virtualPath, int startPosition, int length, bool isLiteral)
        {
            BeginContext(PageContext.Page.GetOutputWriter(), virtualPath, startPosition, length, isLiteral);
        }

        public static void BeginContext(TextWriter writer, string virtualPath, int startPosition, int length, bool isLiteral)
        {
            // Double check that the instrumentation service is active because WriteAttribute always calls this
            if (InstrumentationService.IsAvailable)
            {
                InstrumentationService.BeginContext(Context,
                                                    virtualPath,
                                                    writer,
                                                    startPosition,
                                                    length,
                                                    isLiteral);
            }
        }

        public static void EndContext(string virtualPath, int startPosition, int length, bool isLiteral)
        {
            EndContext(PageContext.Page.GetOutputWriter(), virtualPath, startPosition, length, isLiteral);
        }

        public static void EndContext(TextWriter writer, string virtualPath, int startPosition, int length, bool isLiteral)
        {
            // Double check that the instrumentation service is active because WriteAttribute always calls this
            if (InstrumentationService.IsAvailable)
            {
                InstrumentationService.EndContext(Context,
                                                  virtualPath,
                                                  writer,
                                                  startPosition,
                                                  length,
                                                  isLiteral);
            }
        }
    }
}