0

So I'm relatively new to Dot Net Core stuff so bear with me.

My end goal is to call C# code from Javascript code in my project and vice-versa. So if there's a simpler way than what I'm doing then that would be awesome. I'm trying to follow the tutorial here to run a C# function from js:

https://learn.microsoft.com/en-us/aspnet/core/blazor/call-dotnet-from-javascript?view=aspnetcore-3.1#static-net-method-call

The issue I'm having is in the second window of code on the webpage, here is the line (it is javascript code):

DotNet.invokeMethodAsync('BlazorSample', 'ReturnArrayAsync')
      .then(data => {
        data.push(4);
          console.log(data);
      });

I am getting an error when trying to run this function on my own dot net core site: "Uncaught ReferenceError: DotNet is not defined". I've been googling as many things as I can, and I can't seem to find any reasons for having that error. I can't find any import/require type statements I could put and since I'm new to DotNet Core I'm really stuck. Does anyone know how I can get this module in the javascript, or other ways I can achieve this?

2
  • 1
    Are you creating an AspNetCore (MVC perhaps?) site, or are you creating a Blazor site? The js you quote (in fact that whole example) is all about Blazor. Commented Jun 16, 2020 at 20:36
  • @SamAxe When I created the project in visual studio, I believe I selected the "ASP.NET Core Web Application" template. So I probably need a different way than this tutorial since I'm not using a Blazor app I take it? Commented Jun 16, 2020 at 21:03

1 Answer 1

5

If your only goal is to run C# code on the server called from a client side javascript, then you should look at SignalR (introduction to SignalR here)

The documentation you referenced is for Blazor, for building client apps with C#.

Are you using a sample project? (for example: Blazor WebAssembly Sample App)

If you are not running a sample project: have you included the Blazor webassembly script before your own script?

<script src="_framework/blazor.webassembly.js"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I didn't realize that was a blazor tutorial- and may have also confused blazor with razor. I am doing a ASP.NET Core visual studio template, and it looks like the SignalR you referenced will work for what I need!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.