dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.2k stars 9.95k forks source link

Blazor - Accessing an API controller endpoint triggers Routes.razor #57535

Open mrlife opened 3 weeks ago

mrlife commented 3 weeks ago

Is there an existing issue for this?

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

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

Here is the related information in Program.cs:

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

var app = builder.Build();
app.MapRazorComponents<App>().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

mrlife commented 2 weeks ago

Hi, could the label be corrected to https://github.com/dotnet/aspnetcore/labels/area-blazor?