diff --git a/aspnetcore/blazor/components/cascading-values-and-parameters.md b/aspnetcore/blazor/components/cascading-values-and-parameters.md index 170f9adfe97f..8e41e1333535 100644 --- a/aspnetcore/blazor/components/cascading-values-and-parameters.md +++ b/aspnetcore/blazor/components/cascading-values-and-parameters.md @@ -124,12 +124,11 @@ Create an `ITab` interface that tabs implement in a folder named `UIInterfaces`. ```csharp using Microsoft.AspNetCore.Components; -namespace BlazorSample.UIInterfaces +namespace BlazorSample.UIInterfaces; + +public interface ITab { - public interface ITab - { - RenderFragment ChildContent { get; } - } + RenderFragment ChildContent { get; } } ``` @@ -368,12 +367,11 @@ Create an `ITab` interface that tabs implement in a folder named `UIInterfaces`. ```csharp using Microsoft.AspNetCore.Components; -namespace BlazorSample.UIInterfaces +namespace BlazorSample.UIInterfaces; + +public interface ITab { - public interface ITab - { - RenderFragment ChildContent { get; } - } + RenderFragment ChildContent { get; } } ``` diff --git a/aspnetcore/blazor/components/index.md b/aspnetcore/blazor/components/index.md index c38fe25ac3b8..7a0644f7b264 100644 --- a/aspnetcore/blazor/components/index.md +++ b/aspnetcore/blazor/components/index.md @@ -178,16 +178,15 @@ The following `Counter` component splits HTML and Razor markup from C# code usi `Pages/CounterPartialClass.razor.cs`: ```csharp -namespace BlazorSample.Pages +namespace BlazorSample.Pages; + +public partial class CounterPartialClass { - public partial class CounterPartialClass - { - private int currentCount = 0; + private int currentCount = 0; - private void IncrementCount() - { - currentCount++; - } + private void IncrementCount() + { + currentCount++; } } ``` @@ -1938,16 +1937,15 @@ The following `Counter` component splits HTML and Razor markup from C# code usi `Pages/CounterPartialClass.razor.cs`: ```csharp -namespace BlazorSample.Pages +namespace BlazorSample.Pages; + +public partial class CounterPartialClass { - public partial class CounterPartialClass - { - private int currentCount = 0; + private int currentCount = 0; - private void IncrementCount() - { - currentCount++; - } + private void IncrementCount() + { + currentCount++; } } ``` diff --git a/aspnetcore/blazor/file-uploads.md b/aspnetcore/blazor/file-uploads.md index 52156aadd908..b42f26c53a4c 100644 --- a/aspnetcore/blazor/file-uploads.md +++ b/aspnetcore/blazor/file-uploads.md @@ -288,15 +288,14 @@ The following `UploadResult` class in the **:::no-loc text="Shared":::** project :::moniker range=">= aspnetcore-6.0" ```csharp -namespace BlazorSample.Shared +namespace BlazorSample.Shared; + +public class UploadResult { - public class UploadResult - { - public bool Uploaded { get; set; } - public string? FileName { get; set; } - public string? StoredFileName { get; set; } - public int ErrorCode { get; set; } - } + public bool Uploaded { get; set; } + public string? FileName { get; set; } + public string? StoredFileName { get; set; } + public int ErrorCode { get; set; } } ``` diff --git a/aspnetcore/blazor/forms-and-input-components.md b/aspnetcore/blazor/forms-and-input-components.md index 67db0bef0233..a9c016b4c63f 100644 --- a/aspnetcore/blazor/forms-and-input-components.md +++ b/aspnetcore/blazor/forms-and-input-components.md @@ -772,6 +772,72 @@ The validation for the `Defense` ship classification only occurs server-side in `Controllers/StarshipValidation.cs`: +:::moniker range=">= aspnetcore-6.0" + +```csharp +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Microsoft.Identity.Web.Resource; +using BlazorSample.Shared; + +namespace BlazorSample.Server.Controllers; + +[Authorize] +[ApiController] +[Route("[controller]")] +public class StarshipValidationController : ControllerBase +{ + private readonly ILogger logger; + + public StarshipValidationController( + ILogger logger) + { + this.logger = logger; + } + + static readonly string[] scopeRequiredByApi = new[] { "API.Access" }; + + [HttpPost] + public async Task Post(Starship starship) + { + HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi); + + try + { + if (starship.Classification == "Defense" && + string.IsNullOrEmpty(starship.Description)) + { + ModelState.AddModelError(nameof(starship.Description), + "For a 'Defense' ship " + + "classification, 'Description' is required."); + } + else + { + logger.LogInformation("Processing the form asynchronously"); + + // Process the valid form + // async ... + + return Ok(ModelState); + } + } + catch (Exception ex) + { + logger.LogError("Validation Error: {Message}", ex.Message); + } + + return BadRequest(ModelState); + } +} +``` + +:::moniker-end + +:::moniker range="< aspnetcore-6.0" + ```csharp using System; using System.Threading.Tasks; @@ -833,6 +899,8 @@ namespace BlazorSample.Server.Controllers } ``` +:::moniker-end + If using the preceding controller in a hosted Blazor WebAssembly app, update the namespace (`BlazorSample.Server.Controllers`) to match the app's controllers namespace. When a model binding validation error occurs on the server, an [`ApiController`](xref:web-api/index) () normally returns a [default bad request response](xref:web-api/index#default-badrequest-response) with a . The response contains more data than just the validation errors, as shown in the following example when all of the fields of the `Starfleet Starship Database` form aren't submitted and the form fails validation: diff --git a/aspnetcore/blazor/globalization-localization.md b/aspnetcore/blazor/globalization-localization.md index fea0cfc8c7ac..2710a8571485 100644 --- a/aspnetcore/blazor/globalization-localization.md +++ b/aspnetcore/blazor/globalization-localization.md @@ -893,11 +893,10 @@ To create localization shared resources, adopt the following approach. `Localization/SharedResource.cs`: ```csharp - namespace BlazorSample.Localization + namespace BlazorSample.Localization; + + public class SharedResource { - public class SharedResource - { - } } ``` @@ -1830,11 +1829,10 @@ To create localization shared resources, adopt the following approach. `Localization/SharedResource.cs`: ```csharp - namespace BlazorSample.Localization + namespace BlazorSample.Localization; + + public class SharedResource { - public class SharedResource - { - } } ``` diff --git a/aspnetcore/blazor/host-and-deploy/multiple-hosted-webassembly.md b/aspnetcore/blazor/host-and-deploy/multiple-hosted-webassembly.md index 7e1b884d8f33..6bb43c848b46 100644 --- a/aspnetcore/blazor/host-and-deploy/multiple-hosted-webassembly.md +++ b/aspnetcore/blazor/host-and-deploy/multiple-hosted-webassembly.md @@ -224,6 +224,26 @@ If you plan to serve pages from the server app, add an `Index` Razor page to the `Pages/Index.cshtml.cs`: +:::moniker range=">= aspnetcore-6.0" + +```csharp +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace MultipleBlazorApps.Server.Pages; + +public class IndexModel : PageModel +{ + public void OnGet() + { + } +} +``` + +:::moniker-end + +:::moniker range="< aspnetcore-6.0" + ```csharp using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; @@ -239,6 +259,8 @@ namespace MultipleBlazorApps.Server.Pages } ``` +:::moniker-end + > [!NOTE] > If the app requires additional Razor Pages assets, such as a layout, styles, scripts, and imports, obtain them from an app created from the Razor Pages project template. For more information, see . @@ -259,6 +281,8 @@ If you plan to serve MVC views from the server app, add an `Index` view and a `H `Controllers/HomeController.cs`: +:::moniker range=">= aspnetcore-6.0" + ```csharp using Microsoft.AspNetCore.Mvc; @@ -273,6 +297,27 @@ public class HomeController : Controller } ``` +:::moniker-end + +:::moniker range="< aspnetcore-6.0" + +```csharp +using Microsoft.AspNetCore.Mvc; + +namespace MultipleBlazorApps.Server.Controllers +{ + public class HomeController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} +``` + +:::moniker-end + > [!NOTE] > If the app requires additional MVC assets, such as a layout, styles, scripts, and imports, obtain them from an app created from the MVC project template. For more information, see . diff --git a/aspnetcore/blazor/hybrid/class-libraries.md b/aspnetcore/blazor/hybrid/class-libraries.md index 81a4cfd94e36..a8b8ebba265b 100644 --- a/aspnetcore/blazor/hybrid/class-libraries.md +++ b/aspnetcore/blazor/hybrid/class-libraries.md @@ -119,15 +119,14 @@ The example uses the following specifications and conventions: `Data/WeatherForecast.cs` in the RCL: ```csharp -namespace SharedLibrary.Data +namespace SharedLibrary.Data; + +public class WeatherForecast { - public class WeatherForecast - { - public DateTime Date { get; set; } - public int TemperatureC { get; set; } - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - public string? Summary { get; set; } - } + public DateTime Date { get; set; } + public int TemperatureC { get; set; } + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + public string? Summary { get; set; } } ``` @@ -136,12 +135,11 @@ namespace SharedLibrary.Data ```csharp using SharedLibrary.Data; -namespace SharedLibrary.Interfaces +namespace SharedLibrary.Interfaces; + +public interface IWeatherForecastService { - public interface IWeatherForecastService - { - Task GetForecastAsync(DateTime startDate); - } + Task GetForecastAsync(DateTime startDate); } ``` @@ -159,20 +157,19 @@ using System.Net.Http.Json; using SharedLibrary.Data; using SharedLibrary.Interfaces; -namespace {APP NAMESPACE}.Services -{ - public class WeatherForecastService : IWeatherForecastService - { - private readonly HttpClient http; +namespace {APP NAMESPACE}.Services; - public WeatherForecastService(HttpClient http) - { - this.http = http; - } +public class WeatherForecastService : IWeatherForecastService +{ + private readonly HttpClient http; - public async Task GetForecastAsync(DateTime startDate) => - await http.GetFromJsonAsync("WeatherForecast"); + public WeatherForecastService(HttpClient http) + { + this.http = http; } + + public async Task GetForecastAsync(DateTime startDate) => + await http.GetFromJsonAsync("WeatherForecast"); } ``` @@ -184,24 +181,23 @@ In the preceding example, the `{APP NAMESPACE}` placeholder is the app's namespa using SharedLibrary.Data; using SharedLibrary.Interfaces; -namespace {APP NAMESPACE}.Services +namespace {APP NAMESPACE}.Services; + +public class WeatherForecastService : IWeatherForecastService { - public class WeatherForecastService : IWeatherForecastService + private static readonly string[] Summaries = new[] { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot" - }; - - public async Task GetForecastAsync(DateTime startDate) => - await Task.FromResult(Enumerable.Range(1, 5) - .Select(index => new WeatherForecast - { - Date = startDate.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }).ToArray()); - } + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot" + }; + + public async Task GetForecastAsync(DateTime startDate) => + await Task.FromResult(Enumerable.Range(1, 5) + .Select(index => new WeatherForecast + { + Date = startDate.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }).ToArray()); } ``` @@ -274,12 +270,11 @@ The following example: `Interfaces/ICustomService.cs` in the RCL: ```csharp -namespace SharedLibrary.Interfaces +namespace SharedLibrary.Interfaces; + +public interface ICustomService { - public interface ICustomService - { - string GetMessage(); - } + string GetMessage(); } ``` @@ -290,14 +285,13 @@ In the following examples, the RCL implements the `ICustomService` interface for ```csharp using SharedLibrary.Interfaces; -namespace SharedLibrary.Platforms.Android +namespace SharedLibrary.Platforms.Android; + +public class CustomService : ICustomService { - public class CustomService : ICustomService + public string GetMessage() { - public string GetMessage() - { - return "Android implementation of ICustomService."; - } + return "Android implementation of ICustomService."; } } ``` @@ -307,14 +301,13 @@ namespace SharedLibrary.Platforms.Android ```csharp using SharedLibrary.Interfaces; -namespace SharedLibrary.Platforms.MacCatalyst +namespace SharedLibrary.Platforms.MacCatalyst; + +public class CustomService : ICustomService { - public class CustomService : ICustomService + public string GetMessage() { - public string GetMessage() - { - return "Mac Catalyst implementation of ICustomService."; - } + return "Mac Catalyst implementation of ICustomService."; } } ``` @@ -324,14 +317,13 @@ namespace SharedLibrary.Platforms.MacCatalyst ```csharp using SharedLibrary.Interfaces; -namespace SharedLibrary.Platforms.Windows +namespace SharedLibrary.Platforms.Windows; + +public class CustomService : ICustomService { - public class CustomService : ICustomService + public string GetMessage() { - public string GetMessage() - { - return "Windows implementation of ICustomService."; - } + return "Windows implementation of ICustomService."; } } ``` @@ -341,14 +333,13 @@ namespace SharedLibrary.Platforms.Windows ```csharp using SharedLibrary.Interfaces; -namespace SharedLibrary.Platforms.iOS +namespace SharedLibrary.Platforms.iOS; + +public class CustomService : ICustomService { - public class CustomService : ICustomService + public string GetMessage() { - public string GetMessage() - { - return "iOS implementation of ICustomService."; - } + return "iOS implementation of ICustomService."; } } ``` @@ -360,14 +351,13 @@ In the following example, the RCL implements the `ICustomService` interface for ```csharp using SharedLibrary.Interfaces; -namespace SharedLibrary.Web +namespace SharedLibrary.Web; + +public class CustomService : ICustomService { - public class CustomService : ICustomService + public string GetMessage() { - public string GetMessage() - { - return "Web implementation of ICustomService."; - } + return "Web implementation of ICustomService."; } } ``` diff --git a/aspnetcore/blazor/hybrid/tutorials/windows-forms.md b/aspnetcore/blazor/hybrid/tutorials/windows-forms.md index ba35c1161be3..cdf33a5ac498 100644 --- a/aspnetcore/blazor/hybrid/tutorials/windows-forms.md +++ b/aspnetcore/blazor/hybrid/tutorials/windows-forms.md @@ -234,26 +234,25 @@ blazorWebView1.RootComponents.Add("#app"); > [!NOTE] > The `InitializeComponent` method is generated by a source generator at app build time and added to the compilation object for the calling class. -The final, complete C# code of `Form1.cs`: +The final, complete C# code of `Form1.cs` with a [file-scoped namespace](/dotnet/csharp/language-reference/keywords/namespace): ```csharp using Microsoft.AspNetCore.Components.WebView.WindowsForms; using Microsoft.Extensions.DependencyInjection; -namespace WinFormsBlazor +namespace WinFormsBlazor; + +public partial class Form1 : Form { - public partial class Form1 : Form + public Form1() { - public Form1() - { - InitializeComponent(); - - var services = new ServiceCollection(); - services.AddWindowsFormsBlazorWebView(); - blazorWebView1.HostPage = "wwwroot\\index.html"; - blazorWebView1.Services = services.BuildServiceProvider(); - blazorWebView1.RootComponents.Add("#app"); - } + InitializeComponent(); + + var services = new ServiceCollection(); + services.AddWindowsFormsBlazorWebView(); + blazorWebView1.HostPage = "wwwroot\\index.html"; + blazorWebView1.Services = services.BuildServiceProvider(); + blazorWebView1.RootComponents.Add("#app"); } } ``` diff --git a/aspnetcore/blazor/hybrid/tutorials/wpf.md b/aspnetcore/blazor/hybrid/tutorials/wpf.md index 509dec852cb3..bea70ce7b80f 100644 --- a/aspnetcore/blazor/hybrid/tutorials/wpf.md +++ b/aspnetcore/blazor/hybrid/tutorials/wpf.md @@ -245,7 +245,7 @@ Resources.Add("services", serviceCollection.BuildServiceProvider()); > [!NOTE] > The `InitializeComponent` method is generated by a source generator at app build time and added to the compilation object for the calling class. -The final, complete C# code of `MainWindow.xaml.cs`: +The final, complete C# code of `MainWindow.xaml.cs` with a [file-scoped namespace](/dotnet/csharp/language-reference/keywords/namespace) and comments removed: ```csharp using System; @@ -264,21 +264,17 @@ using System.Windows.Navigation; using System.Windows.Shapes; using Microsoft.Extensions.DependencyInjection; -namespace WpfBlazor +namespace WpfBlazor; + +public partial class MainWindow : Window { - /// - /// Interaction logic for MainWindow.xaml - /// - public partial class MainWindow : Window + public MainWindow() { - public MainWindow() - { - InitializeComponent(); - - var serviceCollection = new ServiceCollection(); - serviceCollection.AddWpfBlazorWebView(); - Resources.Add("services", serviceCollection.BuildServiceProvider()); - } + InitializeComponent(); + + var serviceCollection = new ServiceCollection(); + serviceCollection.AddWpfBlazorWebView(); + Resources.Add("services", serviceCollection.BuildServiceProvider()); } } ``` diff --git a/aspnetcore/blazor/security/includes/usermanager-signinmanager.md b/aspnetcore/blazor/security/includes/usermanager-signinmanager.md index 0c52ebacb7da..775a21348dfc 100644 --- a/aspnetcore/blazor/security/includes/usermanager-signinmanager.md +++ b/aspnetcore/blazor/security/includes/usermanager-signinmanager.md @@ -27,7 +27,10 @@ services.Configure(options => options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier); ``` -The following `WeatherForecastController` logs the when the `Get` method is called: +The following `WeatherForecastController` logs the when the `Get` method is called. + +> [!NOTE] +> The following example uses a [file-scoped namespace](/dotnet/csharp/language-reference/keywords/namespace), which is a C# 10 or later (.NET 6 or later) feature. ```csharp using System; @@ -41,50 +44,49 @@ using Microsoft.Extensions.Logging; using BlazorSample.Server.Models; using BlazorSample.Shared; -namespace BlazorSample.Server.Controllers +namespace BlazorSample.Server.Controllers; + +[Authorize] +[ApiController] +[Route("[controller]")] +public class WeatherForecastController : ControllerBase { - [Authorize] - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase + private readonly UserManager userManager; + + private static readonly string[] Summaries = new[] { - private readonly UserManager userManager; + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", + "Balmy", "Hot", "Sweltering", "Scorching" + }; - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", - "Balmy", "Hot", "Sweltering", "Scorching" - }; + private readonly ILogger logger; + + public WeatherForecastController(ILogger logger, + UserManager userManager) + { + this.logger = logger; + this.userManager = userManager; + } - private readonly ILogger logger; + [HttpGet] + public async Task> Get() + { + var rng = new Random(); - public WeatherForecastController(ILogger logger, - UserManager userManager) + var user = await userManager.GetUserAsync(User); + + if (user != null) { - this.logger = logger; - this.userManager = userManager; + logger.LogInformation($"User.Identity.Name: {user.UserName}"); } - [HttpGet] - public async Task> Get() + return Enumerable.Range(1, 5).Select(index => new WeatherForecast { - var rng = new Random(); - - var user = await userManager.GetUserAsync(User); - - if (user != null) - { - logger.LogInformation($"User.Identity.Name: {user.UserName}"); - } - - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = rng.Next(-20, 55), - Summary = Summaries[rng.Next(Summaries.Length)] - }) - .ToArray(); - } + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); } } ``` diff --git a/aspnetcore/blazor/webassembly-lazy-load-assemblies.md b/aspnetcore/blazor/webassembly-lazy-load-assemblies.md index cf307fbea5f7..4f6e4debb71c 100644 --- a/aspnetcore/blazor/webassembly-lazy-load-assemblies.md +++ b/aspnetcore/blazor/webassembly-lazy-load-assemblies.md @@ -314,6 +314,32 @@ The demonstration in this section: `HandGesture.cs`: +:::moniker range=">= aspnetcore-6.0" + + ```csharp + using Microsoft.Extensions.Logging; + + namespace GrantImaharaRobotControls; + + public static class HandGesture + { + public static bool ThumbUp(Axis axis, ILogger logger) + { + logger.LogInformation("Thumb up gesture. Axis: {Axis}", axis); + + // Code to make robot perform gesture + + return true; + } + } + + public enum Axis { Left, Right } + ``` + +:::moniker-end + +:::moniker range="< aspnetcore-6.0" + ```csharp using Microsoft.Extensions.Logging; @@ -335,6 +361,8 @@ The demonstration in this section: } ``` +:::moniker-end + 1. Add the following component to the root of the RCL project. The component permits the user to submit a left or right hand thumb-up gesture request. `Robot.razor`: