How do you call a .NET method from JavaScript? Explain [JSInvokable] and DotNetObjectReference.
Last updated
Only methods marked
[JSInvokable]can be called from JS. Static ones viaDotNet.invokeMethodAsync('Assembly', 'Method', args).For instance methods, pass
DotNetObjectReference.Create(this)to JS, which callsref.invokeMethodAsync(...)to call back into that specific component (e.g. resize callbacks).Dispose the reference in
Dispose()to avoid leaking the object.
- https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript?view=aspnetcore-10.0
- https://blazor-university.com/javascript-interop/calling-dotnet-from-javascript/
- https://www.telerik.com/blogs/blazor-basics-javascript-interop-calling-net-javascript
- https://swimburger.net/blog/dotnet/communicating-between-dotnet-and-javascript-in-blazor-with-in-browser-samples
- https://oneuptime.com/blog/post/2026-01-30-custom-jsinterop-blazor/view