Live Chat Icon For mobile
Live Chat Icon

How do I call a C# method with parameters from JavaScript in Blazor WebAssembly?

Platform: Blazor| Category : JavaScript Interop, WebAssembly

DotNet.invokeMethod and DotNet.invokeMethodAsync are used to call C# methods with parameters from JavaScript in Blazor WebAssembly.

Syntax to call C# from JavaScript: 

DotNet.invokeMethodAsync(“C# method assembly name”, “C# method name”, “C# method parameter”); 


@page "/"
@inject IJSRuntime JsRuntime;

<button class="btn btn-primary" @onclick="OnButtonClick">Call C# from JS</button><br /><br />


@code {
    private static string content { get; set; }

    [JSInvokable] // Return call back from JavaScript with parameter to C#.
    public static void JStoCSCall(string value)
        content = value;

    private async Task OnButtonClick() // Invoked by button clicking and calls JavaScript function.
        await JsRuntime.InvokeAsync<object>("invokeJSfromCS");


      . . . 
      . . . 
        Function invokeJSfromCS () { 
            var value = "C# Method called from JavaScript with parameter";
            // Invoke to call C# function from JavaScript with parameter.
            DotNet.invokeMethod('BlazorWasmApp', 'JStoCSCall', value); 
</body >

Refer to this documentation for more details.

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.