Calling .NET Instance Methods in ASP.NET Core Blazor Directly from JavaScript

Heaven forbid you'd ever actually want to do this

2022-02-08 14:16:54


Danny has blogged:

I ran into an issue recently where I needed to call some C# code from JavaScript. The Microsoft documentation on JavaScript to .NET interop is very detailed and covers a lot of scenarios including the one that would seem to fit the bill (invoking instance methods), but my requirements meant that I couldn’t use this exact approach.

In the example code the JavaScript isn’t being called directly; it’s .NET calling JavaScript which in turn calls the .NET method you want. The reason for this somewhat circular approach is that we need to pass an instance of our current .NET class to the JavaScript so that it can act upon it and call non-static methods.

Read the full article