2
votes

I am upgrading my ASP.Net MVC Application to ASP.Net Core 3.1

In the existing app I am able to get to the URL that the SignalR connection is coming from by overriding the OnConnected method of the Hub class, and reading Context.Headers("referer"). I would use this to be able to tell on which page of my app each SignalR connection is.

However, in SignalR .Net Core, there is no such header sent.

How can I get to the referring URL for the SignalR connections in .Net Core 3.1?

1
Manually send a message to tell the hub what the client page uri is ? Or expose a Client Method (like GetCurrentPageUri()) to the Hub?itminus

1 Answers

2
votes

How can I get to the referring URL for the SignalR connections in .Net Core 3.1?

To achieve your requirement, you can try to pass the path and filename of the current page as query string while you configure the HubConnection, like below.

var pn = window.location.pathname;
var connection = new signalR.HubConnectionBuilder().withUrl("https://xxxx/chatHub?pagename=" + pn)
    .build();

Then on your hub server, you can get them using following code snippet.

public override async Task OnConnectedAsync()
{
    var httpcontext = Context.GetHttpContext();
    var pname = httpcontext.Request.Query["pagename"];
    var from = httpcontext.Request.Headers["Origin"];

    //code logic here


    await base.OnConnectedAsync();
}

Test Result

enter image description here