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); } }
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); } }
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); }
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); } }
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); } }
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); } }
/// <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)); }
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); } }
/// <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); } }
/// <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); }
/// <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); }
public bool IsSectionDefined(string name) { EnsurePageCanBeRequestedDirectly("IsSectionDefined"); return(PreviousSectionWriters.ContainsKey(name)); }
public bool IsSectionDefined([NotNull] string name) { EnsureMethodCanBeInvoked("IsSectionDefined"); return(PreviousSectionWriters.ContainsKey(name)); }