Răsfoiți Sursa

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]>
Mikel Blanchard 5 ani în urmă
părinte
comite
7e2ed4a656
2 a modificat fișierele cu 9 adăugiri și 17 ștergeri
  1. 6 12
      src/Mvc/Mvc.Razor/src/RazorPage.cs
  2. 3 5
      src/Mvc/Mvc.Razor/test/RazorPageTest.cs

+ 6 - 12
src/Mvc/Mvc.Razor/src/RazorPage.cs

@@ -221,21 +221,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor
                 throw new ArgumentNullException(nameof(sectionName));
                 throw new ArgumentNullException(nameof(sectionName));
             }
             }
 
 
-            if (!PreviousSectionWriters.ContainsKey(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);
+                }
 
 
-            if (_ignoredSections == null)
-            {
-                _ignoredSections = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
+                _ignoredSections.Add(sectionName);
             }
             }
-
-            _ignoredSections.Add(sectionName);
         }
         }
 
 
         /// <inheritdoc />
         /// <inheritdoc />

+ 3 - 5
src/Mvc/Mvc.Razor/test/RazorPageTest.cs

@@ -480,7 +480,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
         }
         }
 
 
         [Fact]
         [Fact]
-        public async Task IgnoreSection_ThrowsIfSectionIsNotFound()
+        public async Task IgnoreSection_DoesNotThrowIfSectionIsNotFound()
         {
         {
             // Arrange
             // Arrange
             var context = CreateViewContext(viewPath: "/Views/TestPath/Test.cshtml");
             var context = CreateViewContext(viewPath: "/Views/TestPath/Test.cshtml");
@@ -496,10 +496,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor
             };
             };
 
 
             // Act & Assert
             // Act & Assert
-            var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => page.ExecuteAsync());
-            var message = $"The layout page '/Views/Shared/_Layout.cshtml' cannot find the section 'bar'" +
-                " in the content page '/Views/TestPath/Test.cshtml'.";
-            Assert.Equal(message, ex.Message);
+            await page.ExecuteAsync();
+            // Nothing to assert, just getting here is validation enough.
         }
         }
 
 
         [Fact]
         [Fact]