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);
            }
        }
Exemple #2
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);
        }
Exemple #3
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);
            }
        }
Exemple #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);
            }
        }
Exemple #5
0
        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);
            }
        }
Exemple #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);
            }
        }