Skip to content

Commit

Permalink
Blazor router NotFound updates (#30329)
Browse files Browse the repository at this point in the history
  • Loading branch information
guardrex committed Sep 12, 2023
1 parent 6da4e2f commit 9b8b944
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
12 changes: 12 additions & 0 deletions aspnetcore/blazor/components/control-head-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,20 @@ When the [`::after` pseudo-selector](https://developer.mozilla.org/docs/Web/CSS/

## Not found page title

:::moniker range=">= aspnetcore-8.0"

*This section only applies to Blazor WebAssembly apps.*

In Blazor apps created from Blazor WebAssembly project template, the `NotFound` component template in the `App` component (`App.razor`) sets the page title to `Not found`.

:::moniker-end

:::moniker range="< aspnetcore-8.0"

In Blazor apps created from Blazor project templates, the `NotFound` component template in the `App` component (`App.razor`) sets the page title to `Not found`.

:::moniker-end

`App.razor`:

```razor
Expand Down
7 changes: 7 additions & 0 deletions aspnetcore/blazor/components/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ Specifying the layout as a default layout in the `Router` component is a useful

To set a layout for arbitrary Razor template content, specify the layout with a <xref:Microsoft.AspNetCore.Components.LayoutView> component. You can use a <xref:Microsoft.AspNetCore.Components.LayoutView> in any Razor component. The following example sets a layout component named `ErrorLayout` for the `MainLayout` component's <xref:Microsoft.AspNetCore.Components.Routing.Router.NotFound> template (`<NotFound>...</NotFound>`).

:::moniker range=">= aspnetcore-8.0"

> [!NOTE]
> The following example is specifically for a Blazor WebAssembly app because Blazor Web Apps don't use the <xref:Microsoft.AspNetCore.Components.Routing.Router.NotFound> template (`<NotFound>...</NotFound>`). However, the template is supported for backward compatibility to avoid a breaking change in the framework. Blazor Web Apps typically process bad URL requests by either displaying the browser's built-in 404 UI or returning a custom 404 page from the ASP.NET Core server via ASP.NET Core middleware (for example, [`UseStatusCodePagesWithRedirects`](xref:fundamentals/error-handling#usestatuscodepageswithredirects) / [API documentation](xref:Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.UseStatusCodePagesWithRedirects%2A)).
:::moniker-end

`App.razor`:

```razor
Expand Down
7 changes: 5 additions & 2 deletions aspnetcore/blazor/fundamentals/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ When the <xref:Microsoft.AspNetCore.Components.Routing.Router> component navigat

## Provide custom content when content isn't found

<!-- UPDATE 8.0 NotFound isn't part of the BWA template ...
confirm if it's a no-op with SSR/BWA -->
:::moniker range=">= aspnetcore-8.0"

*This section only applies to Blazor WebAssembly apps.* Blazor Web Apps don't use the <xref:Microsoft.AspNetCore.Components.Routing.Router.NotFound> template (`<NotFound>...</NotFound>`), but the template is supported for backward compatibility to avoid a breaking change in the framework. Blazor Web Apps typically process bad URL requests by either displaying the browser's built-in 404 UI or returning a custom 404 page from the ASP.NET Core server via ASP.NET Core middleware (for example, [`UseStatusCodePagesWithRedirects`](xref:fundamentals/error-handling#usestatuscodepageswithredirects) / [API documentation](xref:Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.UseStatusCodePagesWithRedirects%2A)).

:::moniker-end

The <xref:Microsoft.AspNetCore.Components.Routing.Router> component allows the app to specify custom content if content isn't found for the requested route.

Expand Down
21 changes: 10 additions & 11 deletions aspnetcore/blazor/security/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,6 @@ If `user.Identity.IsAuthenticated` is `true`, claims can be enumerated and membe

Set up the `Task<`<xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationState>`>` [cascading parameter](xref:blazor/components/cascading-values-and-parameters) using the <xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView> and <xref:Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState> components.

<!-- UPDATE 8.0 Need to confirm this next bit in the BWA world
e.g., did Found survive the NotFound removal? -->

When you create a Blazor app from one of the Blazor project templates with authentication enabled, the app includes the <xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView> and <xref:Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState> components shown in the following example. A client-side Blazor app includes the required service registrations as well. Additional information is presented in the [Customize unauthorized content with the Router component](#customize-unauthorized-content-with-the-router-component) section.

```razor
Expand All @@ -292,7 +289,6 @@ When you create a Blazor app from one of the Blazor project templates with authe
DefaultLayout="@typeof(MainLayout)" />
...
</Found>
...
</Router>
</CascadingAuthenticationState>
```
Expand Down Expand Up @@ -502,9 +498,6 @@ Not authorized.

To authorize users for resources, pass the request's route data to the <xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Resource> parameter of <xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView>.

<!-- UPDATE 8.0 Confirm the following remark in the BWA world
i.e., did Found content survive the NotFound drop? -->

In the <xref:Microsoft.AspNetCore.Components.Routing.Router.Found?displayProperty=nameWithType> content for a requested route:

```razor
Expand Down Expand Up @@ -605,9 +598,6 @@ In the following `EditUser` component, the resource at `/users/{id}/edit` has a

The <xref:Microsoft.AspNetCore.Components.Routing.Router> component, in conjunction with the <xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView> component, allows the app to specify custom content if:

<!-- UPDATE 8.0 React to NotFound changes in the BWA world
the following removed NotFound content and tags -->

* The user fails an [`[Authorize]`](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute) condition applied to the component. The markup of the [`<NotAuthorized>`](xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.NotAuthorized?displayProperty=nameWithType) element is displayed. The [`[Authorize]`](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute) attribute is covered in the [`[Authorize]` attribute](#authorize-attribute) section.
* Asynchronous authorization is in progress, which usually means that the process of authenticating the user is in progress. The markup of the [`<Authorizing>`](xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Authorizing?displayProperty=nameWithType) element is displayed.

Expand All @@ -624,13 +614,22 @@ The <xref:Microsoft.AspNetCore.Components.Routing.Router> component, in conjunct
</Authorizing>
</AuthorizeRouteView>
</Found>
...
</Router>
</CascadingAuthenticationState>
```

:::moniker range=">= aspnetcore-8.0"

The content of `<NotAuthorized>` and `<Authorizing>` tags can include arbitrary items, such as other interactive components.

:::moniker-end

:::moniker range="< aspnetcore-8.0"

The content of `<NotFound>`, `<NotAuthorized>`, and `<Authorizing>` tags can include arbitrary items, such as other interactive components.

:::moniker-end

If the `<NotAuthorized>` tag isn't specified, the <xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView> uses the following fallback message:

```html
Expand Down
4 changes: 0 additions & 4 deletions aspnetcore/blazor/security/server/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ services.AddScoped<AuthenticationStateProvider,

Confirm or add an <xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView> and <xref:Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState> for the Blazor router:

<!-- UPDATE 8.0 Check for dropping NotFound
Did Found survive? -->

```razor
<CascadingAuthenticationState>
<Router ...>
Expand All @@ -208,7 +205,6 @@ Confirm or add an <xref:Microsoft.AspNetCore.Components.Authorization.AuthorizeR
DefaultLayout="@typeof(MainLayout)" />
...
</Found>
...
</Router>
</CascadingAuthenticationState>
```
Expand Down

0 comments on commit 9b8b944

Please sign in to comment.