user2921909
user2921909

Reputation: 93

How to serve blazor app from a controller action in ASP.NET Core

I've previously asked this question for Blazor around the time when the 3.0 preview was out. Is there a way to serve a Blazor app from a specific controller action in a MVC app?

Since then Blazor 3.2 has arrived, I did some research on how to accomplish this, I tried following the examples on this github issue but didn't really manage to get it working.

https://github.com/dotnet/aspnetcore/issues/20642

Thoughts?

Upvotes: 2

Views: 514

Answers (2)

Anthony
Anthony

Reputation: 2824

A mix of Kazbek's answer and this post helped me to integrate blazor wasm to the existing MVC site. Now I have wasm served by a controller action protected by [Authorize].

Upvotes: 0

Kazbek
Kazbek

Reputation: 182

Try this:

public IActionResult MyAction([FromServices] IWebHostEnvironment webHost)
{
    var file = webHost.WebRootFileProvider.GetFileInfo("index.html");
    return PhysicalFile(file.PhysicalPath, "text/html");
}

And change fallback in Startup.cs to:

app.UseEndpoints(endpoints =>
{
    ...
    endpoints.MapFallbackToController("MyAction","controller")
});

Upvotes: 2

Related Questions