summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.WebPages/TemplateStack.cs
blob: 574173997f1f4e30d6ccaee85ebec46293458561 (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
// 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.Linq;

namespace System.Web.WebPages
{
    /// <summary>
    /// Template stacks store a stack of template files. WebPageExecutingBase implements this type, so when executing Plan9 or Mvc WebViewPage,
    /// the stack would contain instances of the page. 
    /// The stack can be queried to identify properties of the current executing file such as the virtual path of the file.
    /// </summary>
    [SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "TemplateStack is a stack")]
    public static class TemplateStack
    {
        private static readonly object _contextKey = new object();

        public static ITemplateFile GetCurrentTemplate(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            return GetStack(httpContext).FirstOrDefault();
        }

        public static ITemplateFile Pop(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            return GetStack(httpContext).Pop();
        }

        public static void Push(HttpContextBase httpContext, ITemplateFile templateFile)
        {
            if (templateFile == null)
            {
                throw new ArgumentNullException("templateFile");
            }
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            GetStack(httpContext).Push(templateFile);
        }

        private static Stack<ITemplateFile> GetStack(HttpContextBase httpContext)
        {
            var stack = httpContext.Items[_contextKey] as Stack<ITemplateFile>;
            if (stack == null)
            {
                stack = new Stack<ITemplateFile>();
                httpContext.Items[_contextKey] = stack;
            }
            return stack;
        }
    }
}