Blazor is a .NET framework that runs in the browser on a real.NET runtime (Mono) via Web Assembly. Blazor is an experimental project and right now, it is not in production and so many changes are going on. Web Assembly is a web standard which defines a binary format just like a lower level assembly language. So, it enables executing code nearly as fast as running native machine code.
It was developed at the W3C (World Wide Web Consortium). It defines an AST (Abstract Syntax Tree) which gets stored in a binary format. It is supported by all the major browsers without any plugins. Mono is a Microsoft .NET Framework implementation based on the ECMA standards for C# and CLR (Common Language Runtime). Mono's runtime is compiled into Web Assembly and its IL interpreter is used to run managed code.
In most conditions, when we do programming with Blazor, C# code will execute in the browser via Web Assembly. Blazor application can also invoke the JavaScript function from C# (.NET) and vice versa. This feature is referred to as "JavaScript Interop".
Calling JavaScript function from C# (.net) code
There are many scenarios when we need to call JavaScript function from Blazor application. Blazor applications use the IJSRuntime interface to call JavaScript from the .NET application. This interface can be accessed using the JSRuntime.Current static property. Using InvokeAsync<T> method of IJSRuntime abstraction, we can call the JavaScript function. This method takes function name and function parameters as the argument.
- Task<T> InvokeAsync<T>(string identifier, params object[] args);
Example
In the following example, I have invoked JavaScript function on the page init method.
- protected override async Task OnInitAsync()
- {
- await JSRuntime.Current.InvokeAsync<object>("CalledJSFunction");
- }
Index.html
- <!DOCTYPE html>
- <html>
- <head>
- ....
- ....
- </head>
- <body>
- <app>Loading...</app>
- <script src="_framework/blazor.webassembly.js"></script>
- <script>
- function CalledJSFunction() {
- alert("Hello Reader!");
- }
- </script>
- </body>
- </html>
We can also pass the parameters to the JavaScript function using the second argument of the InvokeAsync method. This will accept value as objects. In the following Example, I have passed my name as a JavaScript function argument.
- protected override async Task OnInitAsync()
- {
- await JSRuntime.Current.InvokeAsync<object>("CalledJSFunctionWithParameter", "Jignesh Trivedi");
- }
Index.html
- function CalledJSFunctionWithParameter(name) {
- alert("Hi, This is " + name);
- }
Calling .net (C#) function from JavaScript
Blazor application enables us to call .NET (C#) method from JavaScript. There are two JavaScript:DotNet.invokeMethod and DotNet.invokeMethodAsunc functions defined that are used to call the C# method from JavaScript. Here, we also need to pass the assembly containing this function. Following is the definition of the invokeMethodAsync method.
- DotNet.invokeMethodAsync(assemblyName, functionname).then(data => { ... })
To invoke C# method from JavaScript, the method must have the following characteristics.
- The method must be decorated with "JSInvokable" attribute
- The method must be public
- The method may either be static or instance level (this is only supported by Blazor 0.5.0 and above)
- The Identifier for the method must be unique
- The method must be non-generic
Example
The following function is called from the JavaScript. I have defined this function as static within DemoComponent and here, it is just returning a static string from this function.
- //DemoComponent.cs
- [JSInvokable("CalledByComponentScript")]
- public static string CalledByComponentScript()
- {
- return "Hello from C#";
- }
- //Index.html
- function CalledCSFunction() {
- DotNet.invokeMethodAsync("JavaScriptInterop", 'CalledByComponentScript')
- .then(data => {
- alert(data)
- })
- }
demo.cshtml
- <button class="btn btn-success" onclick="CalledCSFunction()">Call Java Script</button>


niaz morshedPosted Jul 21, 2019, 1:52 AM
Nice Article
Sagar Pandurang KapPosted Jan 2, 2019, 11:42 PM
Helpful Content.Thank you..............