Skip to content

Commit 23edcf9

Browse files
committed
Merge branch 'fritz-upgradenet6' of github.com:dotnet-presentations/blazor-workshop into fritz-upgradenet6
2 parents ac69e9a + dd8749c commit 23edcf9

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

docs/03-show-order-status.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ Now when you run the app, you'll be able to visit this page:
5656

5757
Also notice that this time, no full-page load occurs when you navigate, because the URL is matched entirely within the client-side SPA. As such, navigation is instantaneous.
5858

59+
## Adding a page title
60+
61+
In your browser, the title of the new page is listed as **Blazing Pizza** and it would be nice to update the title to reflect that this is the 'My Orders' page. We can use the new `PageTitle` component to update the title from the `MyOrders.razor` page:
62+
63+
```html
64+
@page "/myorders"
65+
66+
<PageTitle>Blazing Pizza - My Orders</PageTitle>
67+
68+
<div class="main">
69+
My orders will go here
70+
</div>
71+
```
72+
73+
This works because inside the `Program.cs` file is an entry that adds a `HeadOutlet` component to the HTML presenting the BlazingPizza application. Blazor uses this `HeadOutlet` to write content into the header of the HTML page.
74+
75+
```csharp
76+
builder.RootComponents.Add<HeadOutlet>("head::after");
77+
```
78+
5979
## Highlighting navigation position
6080

6181
Look closely at the top bar. Notice that when you're on "My orders", the link *isn't* highlighted in yellow. How can we highlight links when the user is on them? By using a `NavLink` component instead of a plain `<a>` tag. The only special thing a `NavLink` component does is toggle its own `active` CSS class depending on whether its `href` matches the current navigation state.

docs/05-checkout-with-validation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ It's time to fix this by adding a "checkout" screen that requires customers to e
99
Start by adding a new page component, `Checkout.razor`, with a `@page` directive matching the URL `/checkout`. For the initial markup, let's display the details of the order using your `OrderReview` component:
1010

1111
```razor
12+
<PageTitle>Blazing Pizza - Checkout</PageTitle>
13+
1214
<div class="main">
1315
<div class="checkout-cols">
1416
<div class="checkout-order-details">

docs/06-authentication-and-authorization.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,48 @@ builder.Services.AddHttpClient<OrdersClient>(client => client.BaseAddress = new
263263
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
264264
```
265265

266+
### Optional: Optimize JSON interactions with .NET 6 JSON CodeGeneration
267+
268+
Starting with .NET 6, the System.Text.Json.JsonSerializer supports working with optimized code generated for serializing and deserializing JSON payloads. Code is generated at build time, resulting in a significant performance improvement for the serialization and deserialization of JSON data. This is configured by taking the following steps:
269+
270+
1. Create a partial Context class that inherits from `System.Text.Json.Serialization.JsonSerializerContext`
271+
2. Decorate the class with the `System.Text.Json.JsonSourceGenerationOptions` attribute
272+
3. Add `JsonSerializable` attributes to the class definition for each type that you would like to have code generated
273+
274+
We have already written this context for you and it is located in the `BlazingPizza.Shared.Order.cs" file
275+
276+
```csharp
277+
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
278+
[JsonSerializable(typeof(Order))]
279+
[JsonSerializable(typeof(OrderWithStatus))]
280+
[JsonSerializable(typeof(List<OrderWithStatus>))]
281+
[JsonSerializable(typeof(Pizza))]
282+
[JsonSerializable(typeof(List<PizzaSpecial>))]
283+
[JsonSerializable(typeof(List<Topping>))]
284+
[JsonSerializable(typeof(Topping))]
285+
public partial class OrderContext : JsonSerializerContext {}
286+
```
287+
288+
You can now optimize the calls to the HttpClient in the `OrdersClient` class by passing an `OrderContext.Default` parameter pointing to the type sought as the second parameter. Updating the methods in the `OrdersClient` class should look like the following:
289+
290+
```csharp
291+
public async Task<IEnumerable<OrderWithStatus>> GetOrders() =>
292+
await httpClient.GetFromJsonAsync("orders", OrderContext.Default.ListOrderWithStatus);
293+
294+
public async Task<OrderWithStatus> GetOrder(int orderId) =>
295+
await httpClient.GetFromJsonAsync($"orders/{orderId}", OrderContext.Default.OrderWithStatus);
296+
297+
public async Task<int> PlaceOrder(Order order)
298+
{
299+
var response = await httpClient.PostAsJsonAsync("orders", order, OrderContext.Default.Order);
300+
response.EnsureSuccessStatusCode();
301+
var orderId = await response.Content.ReadFromJsonAsync<int>();
302+
return orderId;
303+
}
304+
```
305+
306+
### Deploy OrdersClient to pages
307+
266308
Update each page where an `HttpClient` is used to manage orders to use the new typed `OrdersClient`. Inject an `OrdersClient` instead of an `HttpClient` and use the new client to make the API call. Wrap each call in a `try-catch` that handles exceptions of type `AccessTokenNotAvailableException` by calling the provided `Redirect()` method.
267309

268310
*Checkout.razor*

0 commit comments

Comments
 (0)