Search code examples
c#asp.netsignalrsignalr-hubasp.net-core-signalr

How to set query variable in signalR alpha


I'm porting my app from old signalR to new alpha version. On js side I set some variables using

$.connection.hub.qs = {'MyVariable1' : 'val1', 'MyVariable2' : 'val2'}

Then on the signalR server I was able to read these variables with

public class MyHub : Hub
{
    protected (string myVar1, string myVar2) GetValues() =>
            (
            Context.QueryString["MyVariable1"] ?? string.Empty,
            Context.QueryString["MyVariable2"] ?? string.Empty,
            );
}

I found that the server code needs to change to:

var httpContext = Context.Connection.GetHttpContext();
httpContext.Request.Query["MyVariable"]

However I couldn't figure out how to change my js to send query string to the server.


Solution

  • Just pass parameters in the query string you use when creating your hub connection e.g.:

    let hubConnection = new signalR.HubConnection(
        "https://myserver/hub?MyVariable1=var1&MyVariable2=var2