コード例 #1
0
        private async Task <HtmlString> RenderSectionAsyncCore(string sectionName, bool required)
        {
            if (_renderedSections.Contains(sectionName))
            {
                var message = Resources.FormatSectionAlreadyRendered(nameof(RenderSectionAsync), Path, sectionName);
                throw new InvalidOperationException(message);
            }

            RenderAsyncDelegate renderDelegate;

            if (PreviousSectionWriters.TryGetValue(sectionName, out renderDelegate))
            {
                _renderedSections.Add(sectionName);
                await renderDelegate(Output);

                // Return a token value that allows the Write call that wraps the RenderSection \ RenderSectionAsync
                // to succeed.
                return(HtmlString.Empty);
            }
            else if (required)
            {
                // If the section is not found, and it is not optional, throw an error.
                var message = Resources.FormatSectionNotDefined(
                    ViewContext.ExecutingFilePath,
                    sectionName,
                    ViewContext.View.Path);
                throw new InvalidOperationException(message);
            }
            else
            {
                // If the section is optional and not found, then don't do anything.
                return(null);
            }
        }
コード例 #2
0
        private async Task <HtmlString> RenderSectionAsyncCore(string sectionName, bool required)
        {
            if (_renderedSections.Contains(sectionName))
            {
                throw new InvalidOperationException($"Section {sectionName} is already rendered");
            }

            if (PreviousSectionWriters.TryGetValue(sectionName, out var renderDelegate))
            {
                _renderedSections.Add(sectionName);

                await renderDelegate();

                // Return a token value that allows the Write call that wraps the RenderSection \ RenderSectionAsync
                // to succeed.
                return(HtmlString.Empty);
            }
            else if (required)
            {
                throw new InvalidOperationException($"Section {sectionName} is not defined");
            }
            else
            {
                // If the section is optional and not found, then don't do anything.
                return(null);
            }
        }
コード例 #3
0
        private async Task <HtmlString> RenderSectionAsyncCore(string sectionName, bool required)
        {
            if (renderedSections.Contains(sectionName))
            {
                log.Warning("Section {section} already rendered", sectionName);
            }

            if (PreviousSectionWriters.TryGetValue(sectionName, out var renderDelegate))
            {
                renderedSections.Add(sectionName);

                await renderDelegate();

                // Return a token value that allows the Write call that wraps the RenderSection \ RenderSectionAsync
                // to succeed.
                return(HtmlString.Empty);
            }

            if (required)
            {
                // If the section is not found, and it is not optional, throw an error.
                throw new InvalidOperationException("Resources.FormatSectionNotDefined(viewContext.ExecutingFilePath, sectionName, ViewContext.View.Path)");
            }

            // If the section is optional and not found, then don't do anything.
            return(null);
        }
コード例 #4
0
        public HelperResult RenderSection([NotNull] string name, bool required)
        {
            EnsureMethodCanBeInvoked("RenderSection");
            if (_renderedSections.Contains(name))
            {
                throw new InvalidOperationException(Resources.FormatSectionAlreadyRendered("RenderSection", name));
            }

            HelperResult action;

            if (PreviousSectionWriters.TryGetValue(name, out action))
            {
                _renderedSections.Add(name);
                return(action);
            }
            else if (required)
            {
                // If the section is not found, and it is not optional, throw an error.
                throw new InvalidOperationException(Resources.FormatSectionNotDefined(name));
            }
            else
            {
                // If the section is optional and not found, then don't do anything.
                return(null);
            }
        }
コード例 #5
0
ファイル: Page.cs プロジェクト: vasistbhargav/RazorPages
        private async Task <HtmlString> RenderSectionAsyncCore(string sectionName, bool required)
        {
            if (_renderedSections.Contains(sectionName))
            {
                var message = "bleh";
                throw new InvalidOperationException(message);
            }

            RenderAsyncDelegate renderDelegate;

            if (PreviousSectionWriters.TryGetValue(sectionName, out renderDelegate))
            {
                _renderedSections.Add(sectionName);
                await renderDelegate(Output);

                // Return a token value that allows the Write call that wraps the RenderSection \ RenderSectionAsync
                // to succeed.
                return(HtmlString.Empty);
            }
            else if (required)
            {
                // If the section is not found, and it is not optional, throw an error.
                var message = "bleh";
                throw new InvalidOperationException(message);
            }
            else
            {
                // If the section is optional and not found, then don't do anything.
                return(null);
            }
        }
コード例 #6
0
        private async Task <HtmlString> RenderSectionAsyncCore(string sectionName, bool required)
        {
            if (_renderedSections.Contains(sectionName))
            {
                var message = Resources.FormatSectionAlreadyRendered(sectionName);
                throw new InvalidOperationException(message);
            }

            RenderAsyncDelegate renderDelegate;

            if (PreviousSectionWriters.TryGetValue(sectionName, out renderDelegate))
            {
                _renderedSections.Add(sectionName);

                using (var writer = new StringCollectionTextWriter(Output.Encoding))
                {
                    await renderDelegate(writer);

                    // Returning a disposed StringCollectionTextWriter is safe.
                    return(new HtmlString(writer));
                }
            }
            else if (required)
            {
                // If the section is not found, and it is not optional, throw an error.
                throw new InvalidOperationException(Resources.FormatSectionNotDefined(sectionName));
            }
            else
            {
                // If the section is optional and not found, then don't do anything.
                return(null);
            }
        }
コード例 #7
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));
        }
コード例 #8
0
ファイル: WebPageBase.cs プロジェクト: emacslisp/cs
        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);
            }
        }
コード例 #9
0
ファイル: RazorPage.cs プロジェクト: wserr/AspNetCore
        /// <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);
            }
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #12
0
 public bool IsSectionDefined(string name)
 {
     EnsurePageCanBeRequestedDirectly("IsSectionDefined");
     return(PreviousSectionWriters.ContainsKey(name));
 }
コード例 #13
0
ファイル: RazorPage.cs プロジェクト: gitter-badger/Wyam
 public bool IsSectionDefined([NotNull] string name)
 {
     EnsureMethodCanBeInvoked("IsSectionDefined");
     return(PreviousSectionWriters.ContainsKey(name));
 }