Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run a Blazor client page within an ASP.NET Core MVC App?

I've been reading about Razor Pages and the idea of being liberated from JavaScript is very compelling. From what I've gathered, while a Blazor client uses the same libraries as the rest of ASP.NET Core, it is a seperate ASP.NET Core web platform.

Is possible to add a Blazor page within an ASP.NET Core MVC application? Why wasn't it developed as an addition to the existing ASP.NET Core platform, instead of a seperate platform?

like image 695
ATL_DEV Avatar asked Jul 02 '19 14:07

ATL_DEV


People also ask

Can you mix Blazor and MVC?

Blazor applications are component-based. Blazor components can be used in existing ASP.NET MVC applications.

Does Blazor work with .NET Core?

Blazor Server provides support for hosting Razor components on the server in an ASP.NET Core app. UI updates are handled over a SignalR connection. The runtime stays on the server and handles: Executing the app's C# code.

Can you use Blazor with .NET framework?

Blazor can make web development easy and more productive by providing full-stack web development using . NET. It runs in all browsers in real . NET runtime and has full .


1 Answers

No, you can't "run Blazor client page within ASP.NET MVC Core page."

The concept of page in Blazor does not exist, unless you mean a Document Page displayed in the browser.

Blazor is focused around the concept of Components: Blazor Component Model.

Yes, you can embed Blazor Components in an MVC app or Razor Pages App.

This is how to do that:

  1. Add a call to an Html helper method which render the Component. Here the component is one that actually contains the rest of the components in a Blazor application (App.razor), which is why the whole Blazor app is going to be rendered in a Razor Pages Application.

Note: This is not Blazor... In Blazor we've got no Html Helpers

<app>@(await Html.RenderStaticComponentAsync<App>())</app>

Below is a link to the page where it is used: https://github.com/danroth27/ClientSideBlazorWithPrerendering/blob/master/ClientSideBlazorWithPrerendering.Server/Pages/_Host.cshtml

Note that _Host.cshtml is not a Blazor Component. It is, again, a Razor Pages page.

Note that in the sample a whole Blazor app is actually rendered, but of course you can use a normal simple Blazor component (one or more, unrelated components, or related ones, that is, parent component with child components). The principal is the same.

Hope this helps...

like image 86
enet Avatar answered Oct 19 '22 00:10

enet