24 February 2024

Render Blazor components from JavaScript in WordPress

Before having the possibility to create HTML elements (Custom Elements), the Blazor framework has made available since .NET 6 a function allowing to register a Razor component so that it can be called from a Javascript function. And as usual, it’s still very simple ^^

🙂I invite you first to click on this button ⬇️:

To do that :

C# Project : 1 single line

Let’s create a project “Blazor WebAssembly” and in Program.cs add this line:

C#

Then, simply publish this project in the folder of your choice.

Once the project is published, copy the _framework folder to the root of your website:

In WordPress :

For this page I installed the extension named “WPCode Lite” and add this following code to this page like this:

JS

Then I added this Javascript code to call my Blazor component:

JS

Finally in this page, this HTML code to display the button you could test above:

HTML

Leave a Reply