Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aspnetcore/blazor/forms-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ In the following `FormExample4` component, the `HandleValidationRequested` handl

`Pages/FormExample4.razor`:

[!code-razor[](~/blazor/samples/6.0/BlazorSample_WebAssembly/Pages/forms-and-validation/FormExample4.razor?highlight=38,42-53,70)]
[!code-razor[](~/blazor/samples/6.0/BlazorSample_WebAssembly/Pages/forms-and-validation/FormExample4.razor?highlight=38,42-52,72)]

## Data Annotations Validator component and custom validation

Expand Down
19 changes: 6 additions & 13 deletions aspnetcore/blazor/fundamentals/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,6 @@ In the following `App` component example:

Use a <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component in place of HTML hyperlink elements (`<a>`) when creating navigation links. A <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component behaves like an `<a>` element, except it toggles an `active` CSS class based on whether its `href` matches the current URL. The `active` class helps a user understand which page is the active page among the navigation links displayed. Optionally, assign a CSS class name to <xref:Microsoft.AspNetCore.Components.Routing.NavLink.ActiveClass?displayProperty=nameWithType> to apply a custom CSS class to the rendered link when the current route matches the `href`.

The following `NavMenu` component creates a [`Bootstrap`](https://getbootstrap.com/docs/) navigation bar that demonstrates how to use <xref:Microsoft.AspNetCore.Components.Routing.NavLink> components:

[!code-razor[](~/blazor/samples/6.0/BlazorSample_WebAssembly/Shared/routing/NavMenu.razor?highlight=4,9)]

> [!NOTE]
> The `NavMenu` component (`NavMenu.razor`) is provided in the `Shared` folder of an app generated from the [Blazor project templates](xref:blazor/project-structure).

Expand Down Expand Up @@ -643,7 +639,12 @@ The following HTML markup is rendered:

Blazor Server is integrated into [ASP.NET Core Endpoint Routing](xref:fundamentals/routing). An ASP.NET Core app is configured to accept incoming connections for interactive components with <xref:Microsoft.AspNetCore.Builder.ComponentEndpointRouteBuilderExtensions.MapBlazorHub%2A> in `Program.cs`:

[!code-csharp[](~/blazor/samples/6.0/BlazorSample_Server/routing/Program.cs)]
```csharp
app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
```

The typical configuration is to route all requests to a Razor page, which acts as the host for the server-side part of the Blazor Server app. By convention, the *host* page is usually named `_Host.cshtml` in the `Pages` folder of the app.

Expand Down Expand Up @@ -1008,10 +1009,6 @@ In the following `App` component example:

Use a <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component in place of HTML hyperlink elements (`<a>`) when creating navigation links. A <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component behaves like an `<a>` element, except it toggles an `active` CSS class based on whether its `href` matches the current URL. The `active` class helps a user understand which page is the active page among the navigation links displayed. Optionally, assign a CSS class name to <xref:Microsoft.AspNetCore.Components.Routing.NavLink.ActiveClass?displayProperty=nameWithType> to apply a custom CSS class to the rendered link when the current route matches the `href`.

The following `NavMenu` component creates a [`Bootstrap`](https://getbootstrap.com/docs/) navigation bar that demonstrates how to use <xref:Microsoft.AspNetCore.Components.Routing.NavLink> components:

[!code-razor[](~/blazor/samples/5.0/BlazorSample_WebAssembly/Shared/routing/NavMenu.razor?highlight=4,9)]

> [!NOTE]
> The `NavMenu` component (`NavMenu.razor`) is provided in the `Shared` folder of an app generated from the [Blazor project templates](xref:blazor/project-structure).

Expand Down Expand Up @@ -1284,10 +1281,6 @@ To parse a query string's parameters, one approach is to use [`URLSearchParams`]

Use a <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component in place of HTML hyperlink elements (`<a>`) when creating navigation links. A <xref:Microsoft.AspNetCore.Components.Routing.NavLink> component behaves like an `<a>` element, except it toggles an `active` CSS class based on whether its `href` matches the current URL. The `active` class helps a user understand which page is the active page among the navigation links displayed. Optionally, assign a CSS class name to <xref:Microsoft.AspNetCore.Components.Routing.NavLink.ActiveClass?displayProperty=nameWithType> to apply a custom CSS class to the rendered link when the current route matches the `href`.

The following `NavMenu` component creates a [`Bootstrap`](https://getbootstrap.com/docs/) navigation bar that demonstrates how to use <xref:Microsoft.AspNetCore.Components.Routing.NavLink> components:

[!code-razor[](~/blazor/samples/3.1/BlazorSample_WebAssembly/Shared/routing/NavMenu.razor?highlight=4,9)]

> [!NOTE]
> The `NavMenu` component (`NavMenu.razor`) is provided in the `Shared` folder of an app generated from the [Blazor project templates](xref:blazor/project-structure).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Inside the closing `</body>` tag of `wwwroot/index.html` (Blazor WebAssembly) or

`Pages/CallJsExample5.razor`:

[!code-razor[](~/blazor/samples/6.0/BlazorSample_WebAssembly/Pages/call-js-from-dotnet/CallJsExample5.razor?highlight=2-3,25,30,38-40,43)]
[!code-razor[](~/blazor/samples/6.0/BlazorSample_WebAssembly/Pages/call-js-from-dotnet/CallJsExample5.razor?highlight=2-3,25,30,40-42,46)]

## Dynamic content generation scenarios

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static async Task Main(string[] args)
builder.RootComponents.Add<App>("app");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddSingleton<NotifierService>();
builder.Services.AddSingleton<NotifierService>();

await builder.Build().RunAsync();
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static async Task Main(string[] args)
builder.RootComponents.Add<App>("#app");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddSingleton<NotifierService>();
builder.Services.AddSingleton<NotifierService>();

await builder.Build().RunAsync();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/todo"

<PageTitle>Todo</PageTitle>

<h1>Todo (@todos.Count(todo => !todo.IsDone))</h1>

<ul>
Expand All @@ -17,7 +19,7 @@

@code {
private List<TodoItem> todos = new();
private string newTodo;
private string? newTodo;

private void AddTodo()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/todo"

<PageTitle>Todo</PageTitle>

<h1>Todo</h1>

<ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/todo"

<PageTitle>Todo</PageTitle>

<h1>Todo</h1>

<ul>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@code {
private List<TodoItem> todos = new();
private string newTodo;
private string? newTodo;

private void AddTodo()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@code {
private Random r = new();
private string stockSymbol;
private string? stockSymbol;
private decimal price;

private async Task SetStock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

@code {
private Random r = new();
private string stockSymbol;
private string? stockSymbol;
private decimal price;
private JsInteropClasses2 jsClass;
private string result;
private JsInteropClasses2? jsClass;
private string? result;

protected override void OnInitialized()
{
Expand All @@ -32,12 +32,15 @@

private async Task SetStock()
{
stockSymbol =
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
price = r.Next(1, 101);
var interopResult = await jsClass.TickerChanged(stockSymbol, price);
result = $"Result of TickerChanged call for {stockSymbol} at " +
$"{price.ToString("c")}: {interopResult}";
if (jsClass is not null)
{
stockSymbol =
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
price = r.Next(1, 101);
var interopResult = await jsClass.TickerChanged(stockSymbol, price);
result = $"Result of TickerChanged call for {stockSymbol} at " +
$"{price.ToString("c")}: {interopResult}";
}
}

public void Dispose() => jsClass?.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@code {
private string currentHeading = "Initial heading";
private string newHeading;
private string? newHeading;
private string checkedMessage = "Not changed yet";

private void UpdateHeading()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,24 @@
var newUploadResults = await JsonSerializer
.DeserializeAsync<IList<UploadResult>>(responseStream, options);

uploadResults = uploadResults.Concat(newUploadResults).ToList();
if (newUploadResults is not null)
{
uploadResults = uploadResults.Concat(newUploadResults).ToList();
}
}
}

shouldRender = true;
}

private static bool FileUpload(IList<UploadResult> uploadResults,
string fileName, ILogger<FileUpload2> logger, out UploadResult result)
string? fileName, ILogger<FileUpload2> logger, out UploadResult result)
{
result = uploadResults.SingleOrDefault(f => f.FileName == fileName);
result = uploadResults.SingleOrDefault(f => f.FileName == fileName) ?? new();

if (result is null)
if (!result.Uploaded)
{
logger.LogInformation("{FileName} not uploaded (Err: 5)", fileName);
result = new();
result.ErrorCode = 5;
}

Expand All @@ -146,6 +148,6 @@

private class File
{
public string Name { get; set; }
public string? Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
IsValidatedDesign = true,
ProductionDate = new DateTime(2245, 4, 11)
};
private EditContext editContext;
private EditContext? editContext;

protected override void OnInitialized()
{
Expand All @@ -41,12 +41,13 @@

private async Task HandleSubmit()
{
if (editContext.Validate())
if (editContext != null && editContext.Validate())
{
Logger.LogInformation("HandleSubmit called: Form is valid");

// Process the valid form
// await ...
await Task.CompletedTask;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
</EditForm>

@code {
private EditContext editContext;
private EditContext? editContext;
private Holodeck holodeck = new();
private ValidationMessageStore messageStore;
private ValidationMessageStore? messageStore;

protected override void OnInitialized()
{
Expand All @@ -39,15 +39,15 @@
messageStore = new(editContext);
}

private void HandleValidationRequested(object sender,
private void HandleValidationRequested(object? sender,
ValidationRequestedEventArgs args)
{
messageStore.Clear();
messageStore?.Clear();

// Custom validation logic
if (!holodeck.Options)
{
messageStore.Add(() => holodeck.Options, "Select at least one.");
messageStore?.Add(() => holodeck.Options, "Select at least one.");
}
}

Expand All @@ -67,6 +67,9 @@

public void Dispose()
{
editContext.OnValidationRequested -= HandleValidationRequested;
if (editContext is not null)
{
editContext.OnValidationRequested -= HandleValidationRequested;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
</EditForm>

@code {
private CustomValidation customValidation;
private CustomValidation? customValidation;
private Starship starship = new() { ProductionDate = DateTime.UtcNow };

private void HandleValidSubmit()
{
customValidation.ClearErrors();
customValidation?.ClearErrors();

var errors = new Dictionary<string, List<string>>();

Expand All @@ -57,7 +57,7 @@

if (errors.Any())
{
customValidation.DisplayErrors(errors);
customValidation?.DisplayErrors(errors);
}
else
{
Expand Down
Loading