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.56k stars 10.05k forks source link

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

Open mrlife opened 3 months ago

mrlife commented 3 months 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 1 month ago

Hi @mkArtakMSFT @javiercn, please update the label to https://github.com/dotnet/aspnetcore/labels/area-blazor when you have a chance?

(perhaps, the dotnet-issue-labeler prioritized "API" or "controller" over "Blazor" in the title when determining the label)