Skip to content

Commit d51fa0e

Browse files
authored
Access modifiers for cascading params (#36362)
1 parent af8896f commit d51fa0e

File tree

8 files changed

+14
-12
lines changed

8 files changed

+14
-12
lines changed

aspnetcore/blazor/components/cascading-values-and-parameters.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ The following component is used to demonstrate how changing the value of `Notify
241241
private int dalekCount;
242242
243243
[CascadingParameter]
244-
public NotifyingDalek? Dalek { get; set; }
244+
private NotifyingDalek? Dalek { get; set; }
245245
246246
private void Update()
247247
{
@@ -433,6 +433,8 @@ For more information, see the following sections of this article:
433433

434434
To make use of cascading values, descendent components declare cascading parameters using the [`[CascadingParameter]` attribute](xref:Microsoft.AspNetCore.Components.CascadingParameterAttribute). Cascading values are bound to cascading parameters **by type**. Cascading multiple values of the same type is covered in the [Cascade multiple values](#cascade-multiple-values) section later in this article.
435435

436+
The `private` access modifier is recommended for cascading parameters because the parameter should be scoped for use only within the component's class in most cases. When subclassing is required, use the `protected` access modifier.
437+
436438
The following component binds the `ThemeInfo` cascading value to a cascading parameter, optionally using the same name of `ThemeInfo`. The parameter is used to set the CSS class for the **`Increment Counter (Themed)`** button.
437439

438440
`ThemedCounter.razor`:
@@ -657,7 +659,7 @@ Descendent `Tab` components capture the containing `TabSet` as a cascading param
657659
658660
@code {
659661
[CascadingParameter]
660-
public TabSet? ContainerTabSet { get; set; }
662+
private TabSet? ContainerTabSet { get; set; }
661663
662664
[Parameter]
663665
public string? Title { get; set; }

aspnetcore/blazor/components/class-libraries-and-static-server-side-rendering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Reusable components are free to receive an <xref:Microsoft.AspNetCore.Http.HttpC
125125

126126
```csharp
127127
[CascadingParameter]
128-
public HttpContext? Context { get; set; }
128+
private HttpContext? Context { get; set; }
129129
```
130130

131131
The value is `null` during interactive rendering and is only set during static SSR.

aspnetcore/blazor/components/httpcontext.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ uid: blazor/components/httpcontext
2222

2323
```csharp
2424
[CascadingParameter]
25-
public HttpContext? HttpContext { get; set; }
25+
private HttpContext? HttpContext { get; set; }
2626
```
2727

2828
For additional context in *advanced* edge cases&dagger;, see the discussion in the following articles:

aspnetcore/blazor/fundamentals/handle-errors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ To process errors in a component:
596596

597597
```csharp
598598
[CascadingParameter]
599-
public ProcessError? ProcessError { get; set; }
599+
private ProcessError? ProcessError { get; set; }
600600
```
601601

602602
* Call an error processing method in any `catch` block with an appropriate exception type. The example `ProcessError` component only offers a single `LogError` method, but the error processing component can provide any number of error processing methods to address alternative error processing requirements throughout the app. The following `Counter` component `@code` block example includes the `ProcessError` cascading parameter and traps an exception for logging when the count is greater than five:
@@ -606,7 +606,7 @@ To process errors in a component:
606606
private int currentCount = 0;
607607
608608
[CascadingParameter]
609-
public ProcessError? ProcessError { get; set; }
609+
private ProcessError? ProcessError { get; set; }
610610
611611
private void IncrementCount()
612612
{
@@ -689,7 +689,7 @@ To process errors in a component:
689689

690690
```razor
691691
[CascadingParameter]
692-
public ProcessError ProcessError { get; set; }
692+
private ProcessError ProcessError { get; set; }
693693
```
694694

695695
* Call an error processing method in any `catch` block with an appropriate exception type. The example `ProcessError` component only offers a single `LogError` method, but the error processing component can provide any number of error processing methods to address alternative error processing requirements throughout the app.

aspnetcore/blazor/fundamentals/routing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ Apps that implement a custom router can also use `NavigationManager.NotFound`. T
835835
```razor
836836
@code {
837837
[CascadingParameter]
838-
public HttpContext? HttpContext { get; set; }
838+
private HttpContext? HttpContext { get; set; }
839839
840840
private void OnNotFoundEvent(object sender, NotFoundEventArgs e)
841841
{

aspnetcore/blazor/globalization-localization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ Add the following `@code` block to the bottom of the `App` component file:
760760
```razor
761761
@code {
762762
[CascadingParameter]
763-
public HttpContext? HttpContext { get; set; }
763+
private HttpContext? HttpContext { get; set; }
764764
765765
protected override void OnInitialized()
766766
{
@@ -1350,7 +1350,7 @@ Add the following `@code` block to the bottom of the `App` component file:
13501350
```razor
13511351
@code {
13521352
[CascadingParameter]
1353-
public HttpContext? HttpContext { get; set; }
1353+
private HttpContext? HttpContext { get; set; }
13541354
13551355
protected override void OnInitialized()
13561356
{

aspnetcore/blazor/hybrid/root-component-parameters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ The following `Keypad` component example:
230230
231231
@code {
232232
[CascadingParameter]
233-
protected KeypadViewModel KeypadViewModel { get; set; }
233+
private KeypadViewModel KeypadViewModel { get; set; }
234234
235235
private void DeleteChar()
236236
{

aspnetcore/blazor/security/content-security-policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ The rendered import map from the `ImportMap` component is generated by the app a
459459
private string? integrity;
460460
461461
[CascadingParameter]
462-
public HttpContext? HttpContext { get; set; }
462+
private HttpContext? HttpContext { get; set; }
463463
464464
protected override void OnInitialized()
465465
{

0 commit comments

Comments
 (0)