Example #1
0
        /// <summary>
        /// Returns a value that indicates whether the specified section is defined in the content page.
        /// </summary>
        /// <param name="name">The section name to search for.</param>
        /// <returns><c>true</c> if the specified section is defined in the content page; otherwise, <c>false</c>.</returns>
        public bool IsSectionDefined(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            EnsureMethodCanBeInvoked(nameof(IsSectionDefined));
            return(PreviousSectionWriters.ContainsKey(name));
        }
Example #2
0
        public HelperResult RenderSection(string name, bool required)
        {
            EnsurePageCanBeRequestedDirectly("RenderSection");

            if (PreviousSectionWriters.ContainsKey(name))
            {
                var result = new HelperResult(tw =>
                {
                    if (_renderedSections.Contains(name))
                    {
                        throw new HttpException(String.Format(CultureInfo.InvariantCulture, WebPageResources.WebPage_SectionAleadyRendered, name));
                    }
                    var body = PreviousSectionWriters[name];
                    // Since the body can also call RenderSection, we need to temporarily remove
                    // the current sections from the stack.
                    var top = SectionWritersStack.Pop();

                    bool pushed = false;
                    try
                    {
                        if (Output != tw)
                        {
                            OutputStack.Push(tw);
                            pushed = true;
                        }

                        body();
                    }
                    finally
                    {
                        if (pushed)
                        {
                            OutputStack.Pop();
                        }
                    }
                    SectionWritersStack.Push(top);
                    _renderedSections.Add(name);
                });
                return(result);
            }
            else if (required)
            {
                // If the section is not found, and it is not optional, throw an error.
                throw new HttpException(String.Format(CultureInfo.InvariantCulture, WebPageResources.WebPage_SectionNotDefined, name));
            }
            else
            {
                // If the section is optional and not found, then don't do anything.
                return(null);
            }
        }
Example #3
0
        /// <summary>
        /// In layout pages, ignores rendering the content of the section named <paramref name="sectionName"/>.
        /// </summary>
        /// <param name="sectionName">The section to ignore.</param>
        public void IgnoreSection(string sectionName)
        {
            if (sectionName == null)
            {
                throw new ArgumentNullException(nameof(sectionName));
            }

            if (PreviousSectionWriters.ContainsKey(sectionName))
            {
                if (_ignoredSections == null)
                {
                    _ignoredSections = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                }

                _ignoredSections.Add(sectionName);
            }
        }
Example #4
0
        /// <summary>
        /// In layout pages, ignores rendering the content of the section named <paramref name="sectionName"/>.
        /// </summary>
        /// <param name="sectionName">The section to ignore.</param>
        public void IgnoreSection(string sectionName)
        {
            if (sectionName == null)
            {
                throw new ArgumentNullException(nameof(sectionName));
            }

            if (!PreviousSectionWriters.ContainsKey(sectionName))
            {
                throw new InvalidOperationException($"Section {sectionName} is not defined");
            }

            if (_ignoredSections == null)
            {
                _ignoredSections = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            }

            _ignoredSections.Add(sectionName);
        }
Example #5
0
        /// <summary>
        /// In layout pages, ignores rendering the content of the section named <paramref name="sectionName"/>.
        /// </summary>
        /// <param name="sectionName">The section to ignore.</param>
        public void IgnoreSection(string sectionName)
        {
            if (sectionName == null)
            {
                throw new ArgumentNullException(nameof(sectionName));
            }

            if (!PreviousSectionWriters.ContainsKey(sectionName))
            {
                // If the section is not defined, throw an error.
                throw new InvalidOperationException("Resources.FormatSectionNotDefined(ViewContext.ExecutingFilePath,sectionName,ViewContext.View.Path)");
            }

            if (ignoredSections == null)
            {
                ignoredSections = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            }

            ignoredSections.Add(sectionName);
        }
Example #6
0
 public bool IsSectionDefined(string name)
 {
     EnsurePageCanBeRequestedDirectly("IsSectionDefined");
     return(PreviousSectionWriters.ContainsKey(name));
 }
Example #7
0
 public bool IsSectionDefined([NotNull] string name)
 {
     EnsureMethodCanBeInvoked("IsSectionDefined");
     return(PreviousSectionWriters.ContainsKey(name));
 }