简体   繁体   中英

asp.net core 3.1 SignalR: pass complex object from javascript client to Hub

I'm trying to pass an object from client to hub: On client:

connection.invoke('MyMethod', {
                i: 1,
                a: 25
            });

On hub:

        public async Task MyMethod(TestModel model)
        {
            // logic
        }

Model:

public class TestModel
{
    [JsonProperty("i")]
    public double Min {get;set;}
    [JsonProperty("a")]
    public double Max {get;set;}
}

In the MyMethod the model is not null, but the values are always 0.

What I'm doing wrong?

According to your description, you should use Newtonsoft.Json in an ASP.NET Core 3.0 SignalR project, since the asp.net core doesn't use Newtonsoft.Json by default.

You should install the Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson NuGet package and then modify the startp.cs ConfigureServices method as below:

services.AddSignalR()
    .AddNewtonsoftJsonProtocol();

More details, you could refer to this article .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM