dotnet / dotnet/aspnetcore

Blazor - Accessing an API controller endpoint triggers Routes.razor

Open
#57,535 1 comment 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

In an ASP.NET Core 8 Blazor web app, I have an API controller with the following setup. When the endpoint is accessed, for some reason, Routes.razor is being run (not sure if App.razor is being run prior to that).

Is this the intended behavior, either by default or with my configuration below, or possibly a bug?

MyController.cs
```c#
[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
[HttpGet]
public void MyMethod()
{
// code here
}
}

```

Here is the related information in Program.cs:

```c#
var builder = WebApplication.CreateBuilder(options);
builder.Services.AddControllers();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddServerSideBlazor();

var app = builder.Build();
app.MapRazorComponents().AddInteractiveServerRenderMode();
app.MapControllers();
```

### Expected Behavior

When accessing an API endpoint, Blazor should not be involved.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

8.0.204

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.