How can I pass/retreive querystring values in .NET Core version of SignalR?

Viewed 116

I'm rewriting an existing .NET Framework application to a .NET Core application. In the old version which uses SignalR 2.x I was able to set querystring values for the connection on the client side:

$.connection.hub.qs = { appId: 123, instanceId: 456 };

And then in the hub itself (server side) I was able to retreive the querystring values like this:

int.TryParse(Context.QueryString["appId"]), out var appId);

However the HubCallerContext no longer has a QueryString NameValueConnection in .NET Core. How can I set/retreive querystring values in .NET Core version of SingalR? Is there a better way now to pass a parameter that is required for all methods on specific hub?

1 Answers

To achieve this in .NET Core in your client side code where you create the hub connection you need to do the following:

connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatHub?appId=123")
    .withAutomaticReconnect()
    .build();

Then in your hub logic you can retreive the query string value like this:

int.TryParse(Context.GetHttpContext().Request.Query["appId"], out var appId);
Related