Skip to content

Commit 7e2ed4a

Browse files
CodeBlanchpranavkm
andauthored
RazorPage: Allow IgnoreSection to specify sections that do not exist (#29742)
* Added an IgnoreSection overload with a required parameter. * Update PublicAPI.Unshipped.txt * Code review. Co-authored-by: Pranav K <[email protected]>
1 parent 457ade0 commit 7e2ed4a

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

src/Mvc/Mvc.Razor/src/RazorPage.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,15 @@ public void IgnoreSection(string sectionName)
221221
throw new ArgumentNullException(nameof(sectionName));
222222
}
223223

224-
if (!PreviousSectionWriters.ContainsKey(sectionName))
224+
if (PreviousSectionWriters.ContainsKey(sectionName))
225225
{
226-
// If the section is not defined, throw an error.
227-
throw new InvalidOperationException(Resources.FormatSectionNotDefined(
228-
ViewContext.ExecutingFilePath,
229-
sectionName,
230-
ViewContext.View.Path));
231-
}
226+
if (_ignoredSections == null)
227+
{
228+
_ignoredSections = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
229+
}
232230

233-
if (_ignoredSections == null)
234-
{
235-
_ignoredSections = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
231+
_ignoredSections.Add(sectionName);
236232
}
237-
238-
_ignoredSections.Add(sectionName);
239233
}
240234

241235
/// <inheritdoc />

src/Mvc/Mvc.Razor/test/RazorPageTest.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public async Task RenderSection_ThrowsIfRequiredSectionIsNotFound()
480480
}
481481

482482
[Fact]
483-
public async Task IgnoreSection_ThrowsIfSectionIsNotFound()
483+
public async Task IgnoreSection_DoesNotThrowIfSectionIsNotFound()
484484
{
485485
// Arrange
486486
var context = CreateViewContext(viewPath: "/Views/TestPath/Test.cshtml");
@@ -496,10 +496,8 @@ public async Task IgnoreSection_ThrowsIfSectionIsNotFound()
496496
};
497497

498498
// Act & Assert
499-
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => page.ExecuteAsync());
500-
var message = $"The layout page '/Views/Shared/_Layout.cshtml' cannot find the section 'bar'" +
501-
" in the content page '/Views/TestPath/Test.cshtml'.";
502-
Assert.Equal(message, ex.Message);
499+
await page.ExecuteAsync();
500+
// Nothing to assert, just getting here is validation enough.
503501
}
504502

505503
[Fact]

0 commit comments

Comments
 (0)