Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File-scoped namespaces #28436

Merged
merged 1 commit into from
Feb 17, 2023
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
18 changes: 8 additions & 10 deletions aspnetcore/blazor/components/cascading-values-and-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
```

Expand Down Expand Up @@ -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; }
}
```

Expand Down
30 changes: 14 additions & 16 deletions aspnetcore/blazor/components/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
```
Expand Down Expand Up @@ -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++;
}
}
```
Expand Down
15 changes: 7 additions & 8 deletions aspnetcore/blazor/file-uploads.md
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
```

Expand Down
68 changes: 68 additions & 0 deletions aspnetcore/blazor/forms-and-input-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<StarshipValidationController> logger;

public StarshipValidationController(
ILogger<StarshipValidationController> logger)
{
this.logger = logger;
}

static readonly string[] scopeRequiredByApi = new[] { "API.Access" };

[HttpPost]
public async Task<IActionResult> 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;
Expand Down Expand Up @@ -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) (<xref:Microsoft.AspNetCore.Mvc.ApiControllerAttribute>) normally returns a [default bad request response](xref:web-api/index#default-badrequest-response) with a <xref:Microsoft.AspNetCore.Mvc.ValidationProblemDetails>. 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:
Expand Down
14 changes: 6 additions & 8 deletions aspnetcore/blazor/globalization-localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
}
```

Expand Down Expand Up @@ -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
{
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <xref:razor-pages/index>.

Expand All @@ -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;

Expand All @@ -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 <xref:tutorials/first-mvc-app/start-mvc>.

Expand Down
Loading