DotNet.invokeMethodAsync('{ASSEMBLY NAME}', '{.NET METHOD ID}', {ARGUMENTS});
- The
{ASSEMBLY NAME}
placeholder is the app’s assembly name. - The
{.NET METHOD ID}
placeholder is the .NET method identifier. - The
{ARGUMENTS}
placeholder are optional, comma-separated arguments to pass to the method, each of which must be JSON-serializable.
@page "/call-dotnet-example-1" <h1>Call .NET Example 1</h1> <p> <button onclick="returnArrayAsync()"> Trigger .NET static method </button> </p> @code { [JSInvokable] public static Task<int[]> ReturnArrayAsync() { return Task.FromResult(new int[] { 1, 2, 3 }); } }
<script> window.returnArrayAsync = () => { DotNet.invokeMethodAsync('BlazorSample', 'ReturnArrayAsync') .then(data => { console.log(data); }); }; </script>
References
https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript?view=aspnetcore-6.0#invoke-a-static-net-method
https://blazor-university.com/javascript-interop/calling-dotnet-from-javascript/calling-static-dotnet-methods/