Skip to content

Commit

Permalink
Blazor minimal API updates (dotnet#23483)
Browse files Browse the repository at this point in the history
  • Loading branch information
guardrex authored Oct 7, 2021
1 parent 3756270 commit 48cb2cf
Show file tree
Hide file tree
Showing 35 changed files with 371 additions and 421 deletions.
24 changes: 12 additions & 12 deletions aspnetcore/blazor/call-web-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Reference the [`System.Net.Http.Json`](https://www.nuget.org/packages/System.Net

## Add the `HttpClient` service

In `Program.Main`, add an <xref:System.Net.Http.HttpClient> service if it isn't already present from a Blazor project template used to create the app:
In `Program.cs`, add an <xref:System.Net.Http.HttpClient> service if it isn't already present from a Blazor project template used to create the app:

```csharp
builder.Services.AddScoped(sp =>
Expand Down Expand Up @@ -210,7 +210,7 @@ Reference the [`Microsoft.Extensions.Http`](https://www.nuget.org/packages/Micro

In the preceding example, the `{VERSION}` placeholder is the version of the package.

In `Program.Main` of the `Program.cs` file:
In `Program.cs`:

```csharp
builder.Services.AddHttpClient("WebAPI", client =>
Expand Down Expand Up @@ -303,7 +303,7 @@ public class WeatherForecastHttpClient
}
```

In `Program.Main` of the `Program.cs` file:
In `Program.cs`:

```csharp
builder.Services.AddHttpClient<WeatherForecastHttpClient>(client =>
Expand Down Expand Up @@ -389,7 +389,7 @@ For more information on Fetch API options, see [MDN web docs: WindowOrWorkerGlob

The following example calls a web API. The example requires a running web API based on the sample app described by the <xref:tutorials/first-web-api> article. This example makes requests to the web API at `https://localhost:10000/api/TodoItems`. If a different web API address is used, update the `ServiceEndpoint` constant value in the component's `@code` block.

The following example makes a [cross-origin resource sharing (CORS)](xref:security/cors) request from `http://localhost:5000` or `https://localhost:5001` to the web API. Add the following CORS middleware configuration to the web API's service's `Startup.Configure` method:
The following example makes a [cross-origin resource sharing (CORS)](xref:security/cors) request from `http://localhost:5000` or `https://localhost:5001` to the web API. Add the following CORS middleware configuration to the web API's service's `Program.cs` file:

```csharp
app.UseCors(policy =>
Expand Down Expand Up @@ -484,10 +484,10 @@ For more information, see <xref:blazor/fundamentals/handle-errors>.

A Blazor Server app doesn't include an <xref:System.Net.Http.HttpClient> service by default. Provide an <xref:System.Net.Http.HttpClient> to the app using the [`HttpClient` factory infrastructure](xref:fundamentals/http-requests).

In `Startup.`ConfigureServices` of `Startup.cs`:
In `Program.cs`:

```csharp
services.AddHttpClient();
builder.Services.AddHttpClient();
```

The following Blazor Server Razor component makes a request to a web API for GitHub branches similar to the *Basic Usage* example in the <xref:fundamentals/http-requests> article.
Expand Down Expand Up @@ -641,7 +641,7 @@ Reference the [`System.Net.Http.Json`](https://www.nuget.org/packages/System.Net

## Add the `HttpClient` service

In `Program.Main`, add an <xref:System.Net.Http.HttpClient> service if it isn't already present from a Blazor project template used to create the app:
In `Program.cs`, add an <xref:System.Net.Http.HttpClient> service if it isn't already present from a Blazor project template used to create the app:

```csharp
builder.Services.AddScoped(sp =>
Expand Down Expand Up @@ -806,7 +806,7 @@ Reference the [`Microsoft.Extensions.Http`](https://www.nuget.org/packages/Micro

In the preceding example, the `{VERSION}` placeholder is the version of the package.

In `Program.Main` of the `Program.cs` file:
In `Program.cs`:

```csharp
builder.Services.AddHttpClient("WebAPI", client =>
Expand Down Expand Up @@ -899,7 +899,7 @@ public class WeatherForecastHttpClient
}
```

In `Program.Main` of the `Program.cs` file:
In `Program.cs`:

```csharp
builder.Services.AddHttpClient<WeatherForecastHttpClient>(client =>
Expand Down Expand Up @@ -1239,7 +1239,7 @@ Reference the [`System.Net.Http.Json`](https://www.nuget.org/packages/System.Net

## Add the `HttpClient` service

In `Program.Main`, add an <xref:System.Net.Http.HttpClient> service if it isn't already present from a Blazor project template used to create the app:
In `Program.cs`, add an <xref:System.Net.Http.HttpClient> service if it isn't already present from a Blazor project template used to create the app:

```csharp
builder.Services.AddScoped(sp =>
Expand Down Expand Up @@ -1404,7 +1404,7 @@ Reference the [`Microsoft.Extensions.Http`](https://www.nuget.org/packages/Micro

In the preceding example, the `{VERSION}` placeholder is the version of the package.

In `Program.Main` of the `Program.cs` file:
In `Program.cs`:

```csharp
builder.Services.AddHttpClient("WebAPI", client =>
Expand Down Expand Up @@ -1497,7 +1497,7 @@ public class WeatherForecastHttpClient
}
```

In `Program.Main` of the `Program.cs` file:
In `Program.cs`:

```csharp
builder.Services.AddHttpClient<WeatherForecastHttpClient>(client =>
Expand Down
2 changes: 1 addition & 1 deletion aspnetcore/blazor/components/control-head-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Specify the page's title with the `PageTitle` component. Specify `<head>` elemen

The `HeadOutlet` component renders content provided by `HeadContent` components.

In an app created from the Blazor WebAssembly project template, the `HeadOutlet` component is added to the <xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.RootComponents> collection of the <xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder> in `Program.Main`:
In an app created from the Blazor WebAssembly project template, the `HeadOutlet` component is added to the <xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.RootComponents> collection of the <xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder> in `Program.cs`:

```csharp
builder.RootComponents.Add<HeadOutlet>("head::after");
Expand Down
14 changes: 7 additions & 7 deletions aspnetcore/blazor/components/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,18 +665,18 @@ In the event a component must be updated based on an external event, such as a t

Register the services:

* In a Blazor WebAssembly app, register the services as singletons in `Program.Main`:
* In a Blazor WebAssembly app, register the services as singletons in `Program.cs`:

```csharp
builder.Services.AddSingleton<NotifierService>();
builder.Services.AddSingleton<TimerService>();
```

* In a Blazor Server app, register the services as scoped in `Startup.ConfigureServices`:
* In a Blazor Server app, register the services as scoped in `Program.cs`:

```csharp
services.AddScoped<NotifierService>();
services.AddScoped<TimerService>();
builder.Services.AddScoped<NotifierService>();
builder.Services.AddScoped<TimerService>();
```

Use the `NotifierService` to update a component.
Expand Down Expand Up @@ -1057,7 +1057,7 @@ await Blazor.rootComponents.add(containerElement, 'counter', { incrementAmount:

## Blazor custom elements

Experimental support is available for building custom elements using the the [`Microsoft.AspNetCore.Components.CustomElements` NuGet package](https://www.nuget.org/packages/microsoft.aspnetcore.components.customelements). Custom elements use standard HTML interfaces to implement custom HTML elements.
Experimental support is available for building custom elements using the [`Microsoft.AspNetCore.Components.CustomElements` NuGet package](https://www.nuget.org/packages/microsoft.aspnetcore.components.customelements). Custom elements use standard HTML interfaces to implement custom HTML elements.

Register a root component as a custom element:

Expand Down Expand Up @@ -1741,7 +1741,7 @@ In the event a component must be updated based on an external event, such as a t

Register the services:

* In a Blazor WebAssembly app, register the services as singletons in `Program.Main`:
* In a Blazor WebAssembly app, register the services as singletons in `Program.cs`:

```csharp
builder.Services.AddSingleton<NotifierService>();
Expand Down Expand Up @@ -2686,7 +2686,7 @@ In the event a component must be updated based on an external event, such as a t

Register the services:

* In a Blazor WebAssembly app, register the services as singletons in `Program.Main`:
* In a Blazor WebAssembly app, register the services as singletons in `Program.cs`:

```csharp
builder.Services.AddSingleton<NotifierService>();
Expand Down
8 changes: 4 additions & 4 deletions aspnetcore/blazor/components/prerendering-and-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ For more information, see <xref:blazor/components/index#namespaces>.

## Preserve prerendered state

Without preserving prerendered state, any state that used during prerendering is lost and must be recreated when the app is fully loaded. If any state is setup asynchronously, the UI may flicker as the the prerendered UI is replaced with temporary placeholders and then fully rendered again.
Without preserving prerendered state, any state that used during prerendering is lost and must be recreated when the app is fully loaded. If any state is setup asynchronously, the UI may flicker as the prerendered UI is replaced with temporary placeholders and then fully rendered again.

To solve these problems, Blazor supports persisting state in a prerendered page using the [Preserve Component State Tag Helper](xref:mvc/views/tag-helpers/builtin-th/preserve-component-state-tag-helper) (`<preserve-component-state />`). Add the `<preserve-component-state />` tag inside the closing `</body>` tag of `_Layout.cshtml`.

Expand Down Expand Up @@ -866,7 +866,7 @@ To set up prerendering for a hosted Blazor WebAssembly app:

1. **Delete** the `wwwroot/index.html` file from the Blazor WebAssembly **`Client`** project.

1. In the **`Client`** project, **delete** the following line in `Program.Main` (`Program.cs`):
1. In the **`Client`** project, **delete** the following line in `Program.cs`:

```diff
- builder.RootComponents.Add<App>("#app");
Expand Down Expand Up @@ -1052,15 +1052,15 @@ Additional work might be required depending on the static resources that compone

## Render components in a page or view with a CSS selector

After [configuring the solution](#solution-configuration), including the [additional configuration](#configuration-for-embedding-razor-components-into-pages-and-views), add root components to the **`Client`** project of a hosted Blazor WebAssembly solution in `Program.Main`. In the following example, the `Counter` component is declared as a root component with a CSS selector that selects the element with the `id` that matches `counter-component`. In the following example, the **`Client`** project's namespace is `BlazorHosted.Client`.
After [configuring the solution](#solution-configuration), including the [additional configuration](#configuration-for-embedding-razor-components-into-pages-and-views), add root components to the **`Client`** project of a hosted Blazor WebAssembly solution in `Program.cs`. In the following example, the `Counter` component is declared as a root component with a CSS selector that selects the element with the `id` that matches `counter-component`. In the following example, the **`Client`** project's namespace is `BlazorHosted.Client`.

In `Program.cs` of the **`Client`** project, add the namespace for the project's Razor components to the top of the file:

```diff
+ using BlazorHosted.Client.Pages;
```

After the `builder` is established in `Program.Main`, add the `Counter` component as a root component:
After the `builder` is established in `Program.cs`, add the `Counter` component as a root component:

```diff
+ builder.RootComponents.Add<Counter>("#counter-component");
Expand Down
Loading

0 comments on commit 48cb2cf

Please sign in to comment.