Blazor is an open source and cross-platform web UI framework for building single-page apps using .NET and C# instead of JavaScript. Blazor is based on a powerful and flexible component model for building rich interactive web UI.
Please read my article on Blazor Introduction - Blazor - C# In Browser
Blazor WebAssembly includes a proper .NET runtime implemented in WebAssembly, a standardized bytecode for the web. This .NET runtime is downloaded with your Blazor WebAssembly app and enables running normal .NET code directly in the browser. No plugins or code transpilation are required. Blazor WebAssembly works with all modern web browsers, both desktop and mobile.
Without the need of JavaScript, developers can build a full stack web application using C#, HTML & CSS. But there are situations where developers need to communicate with an existing JavaScript library. In this article I am going to explain how to call JavaScript from C# in Blazor WebAssembly & vice versa.
Let’s create a sample Blazor WebAssembly app using Visual Studio 2019.
As a first step open visual studio 2019 ->create new project -> select Blazor WebAssembly template with .Net 5 as target runtime, give a project name and click create.
In the solution explorer, you can see a default project template with some components. Under the pages folder you can see there is a Counter.razor component. If we open that, you can see that the HTML and the C# code is tied together. I am not a big fan of this approach hence I am splitting the C# code from the html. In order to achieve that, right click on the pages folder and create a new class named Counter.razor.cs and make the class a partial class. Move C# code from Counter.razor file and paste it in the newly created partial class. Now run the application and see if the counter page is working as expected.
Calling JavaScript From C#
Step 1
Create a JavaScript file named main.js in the wwwroot folder. In the main.js file create two functions. One function accepts a parameter and just simply logs the counter value to a console and the other method return a string message.

Step 2
Open the index.html file and add a reference to the main.js file in the head tag.
Step 3
Add a new folder named JSInterop and add a class named JavascriptInvoker.

Step 4
Create an interface named IJavascriptInvoker with two methods.
a. Task PrintAsync(int counter);
b. Task PrintGreetingMessageAsync()

Step 5
Let JavaScriptInvoker class inherit from IJavascriptInvoker and implement the interface. (IJavascriptInvoker interface is placed on top of JavascriptInvoker class).
Step 6
There is a built-in interface named IJSRuntime which is used to invoke JavaScript methods from C#. This interface is found under the namespace Microsoft.JSInterop. Inject this interface in the constructor.
Step 7
Inject ILogger interface as well for logging.
Step 8
There is a method named InvokeVoidAsync in the IJSRuntime interface. This is an overloaded method. The first argument is the name of the JavaScript function and the second argument is the data that we need to pass to the JS function. In our case the first argument value is “printCounter” and the second argument value is the counter variable as shown in figure below.

Step 9
Similarly, we need to invoke the getGreetingMessage function in the main.js file. For that we need to change the logic inside the PrintGreetingMessageAsync as shown in figure below.

Step 10
As getGreetingMessage function is returning a string, use the InvokeAsync method of IJSRuntime interface. In the method, mention the name of the of the JavaScript method to be called. In our case it is “getGreetingMessage”.
Step 11
Open Program.cs file and add the dependency injection for IJavascriptInvoker.

Step 12
Open the Counter.razor.cs file and inject the IJavascriptInvoker interface. Use property injection. Property injecion is achieved by using the Inject attribute.

Step 13
Invoke the PrintAsync and PrintGreetingMessageAsync inside the IncrementCount method as shown.












Zulqadar IdrishiPosted Apr 8, 2021, 4:35 AM
Nice Article!!!
Vinay AyinapurapuPosted Apr 7, 2021, 4:09 AM
Very nicely written. Thanks for sharing
Anish AravindPosted Apr 6, 2021, 1:58 PM
Very informative.. thanks