Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

c# Blazor `SignalR', how to send complex data such as class

$
0
0

the problem is that when I send one line, everything works, but when I send a class to the server, an empty object arrives

  public class Message  {      public string? User;      public string? Mesg;      public override string ToString()      {          return $"User = {User}; message = {Mesg} ";      }  }  public class ChatHub : Hub  {      public async Task SendMessage(Message _message)      {          await Console.Out.WriteLineAsync(_message.ToString());          await Clients.All.SendAsync("ReceiveMessage", _message);      }  }}

Client :

  private async Task Send()  {      if (hubConnection is not null)      {        await hubConnection.SendAsync("SendMessage", new Message()              {                  User = userInput,                  Mesg = messageInput              });      }  }    protected override async Task OnInitializedAsync()    {        hubConnection = new HubConnectionBuilder()            .WithUrl(Navigation.ToAbsoluteUri("/chathub"))            .Build();        hubConnection.On<Message>("ReceiveMessage", (_message) =>      {          var encodedMsg = $"{_message.User}: {_message.Mesg}";          messages.Add(encodedMsg);          InvokeAsync(StateHasChanged);      });        await hubConnection.StartAsync();    }

how can I transfer complex data?

no matter how I look at the documentation, the video, everyone passes the string everywhere


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>