From 30b6c03ab1ca225c7d694911ea8f251f104c0df8 Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Thu, 12 Feb 2026 08:25:23 -0500 Subject: [PATCH] Configure SignalR hub URL endpoint --- aspnetcore/blazor/fundamentals/signalr.md | 4 +- .../host-and-deploy/proxy-load-balancer.md | 2 + aspnetcore/signalr/configuration.md | 41 ++++++++++++++++++- .../includes/configuration2.1.md | 38 +++++++++++++++++ .../includes/configuration2.2.md | 38 +++++++++++++++++ .../includes/configuration3.1.md | 38 +++++++++++++++++ .../configuration/includes/configuration3.md | 38 +++++++++++++++++ .../configuration/includes/configuration5.md | 38 +++++++++++++++++ .../configuration/includes/configuration6.md | 38 +++++++++++++++++ .../configuration/includes/configuration7.md | 38 +++++++++++++++++ 10 files changed, 309 insertions(+), 4 deletions(-) diff --git a/aspnetcore/blazor/fundamentals/signalr.md b/aspnetcore/blazor/fundamentals/signalr.md index 89b4f563dbb9..a054f0ce5aea 100644 --- a/aspnetcore/blazor/fundamentals/signalr.md +++ b/aspnetcore/blazor/fundamentals/signalr.md @@ -174,7 +174,7 @@ To resolve the problem, use ***either*** of the following approaches: * [Secure a SignalR hub](xref:blazor/security/webassembly/index#secure-a-signalr-hub) * -* +* ([Configure a SignalR hub URL for production](xref:signalr/configuration#configure-the-hub-url-endpoint)) * [Blazor samples GitHub repository (`dotnet/blazor-samples`)](https://github.com/dotnet/blazor-samples) ([how to download](xref:blazor/fundamentals/index#sample-apps)) ## Use session affinity (sticky sessions) for server-side web farm hosting @@ -1803,7 +1803,7 @@ In the preceding code, `NavManager` is a -* +* ([Configure a SignalR hub URL for production](xref:signalr/configuration#configure-the-hub-url-endpoint)) * Server-side security documentation * * diff --git a/aspnetcore/host-and-deploy/proxy-load-balancer.md b/aspnetcore/host-and-deploy/proxy-load-balancer.md index d1e80370614b..7c483ea9601c 100644 --- a/aspnetcore/host-and-deploy/proxy-load-balancer.md +++ b/aspnetcore/host-and-deploy/proxy-load-balancer.md @@ -242,6 +242,7 @@ To display the logs, add `"Microsoft.AspNetCore.HttpLogging": "Information"` to * * [Microsoft Security Advisory CVE-2018-0787: ASP.NET Core Elevation Of Privilege Vulnerability](https://github.com/aspnet/Announcements/issues/295) * [YARP: Yet Another Reverse Proxy](https://dotnet.github.io/yarp/) +* [Configure a SignalR hub URL for production](xref:signalr/configuration#configure-the-hub-url-endpoint) :::moniker-end @@ -581,5 +582,6 @@ services.Configure(options => * * [Microsoft Security Advisory CVE-2018-0787: ASP.NET Core Elevation Of Privilege Vulnerability](https://github.com/aspnet/Announcements/issues/295) * [YARP: Yet Another Reverse Proxy](https://dotnet.github.io/yarp/) +* [Configure a SignalR hub URL for production](xref:signalr/configuration#configure-the-hub-url-endpoint) :::moniker-end diff --git a/aspnetcore/signalr/configuration.md b/aspnetcore/signalr/configuration.md index 4fc29bececb2..cf8a425fd46f 100644 --- a/aspnetcore/signalr/configuration.md +++ b/aspnetcore/signalr/configuration.md @@ -5,10 +5,9 @@ description: Learn how to configure ASP.NET Core SignalR apps. monikerRange: '>= aspnetcore-2.1' ms.author: wpickett ms.custom: mvc -ms.date: 05/02/2024 +ms.date: 02/12/2026 uid: signalr/configuration --- - # ASP.NET Core SignalR configuration This article covers ASP.NET Core SignalR configuration. @@ -131,6 +130,44 @@ The WebSocket transport has additional options that can be configured using the Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. +### Configure the hub URL endpoint + +To set a static hub URL endpoint, pass the URL to . The following example sets the URL to `https://contoso.com/chathub`: + +```csharp +var connection = new HubConnectionBuilder() + .WithUrl("https://contoso.com/chathub") + .Build(); +``` + +To configure the endpoint in code, use to build the URL and pass it to . The following example configures a custom hub URL for production on a target IIS server: + +```csharp +#if DEBUG + // IIS Express development environment (development build) + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .WithAutomaticReconnect() + .Build(); +#else + // IIS server production environment (release build) + var hostName = Environment.MachineName; + var addresses = Dns.GetHostAddresses(hostName); + var ipv4 = addresses.FirstOrDefault(addresses => + addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + var uri = + new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) + { + Scheme = "http", Port = 80, Host = ipv4 + }.ToString(); + + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri(uri)) + .WithAutomaticReconnect() + .Build(); +#endif +``` + ### Configure logging Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information. diff --git a/aspnetcore/signalr/configuration/includes/configuration2.1.md b/aspnetcore/signalr/configuration/includes/configuration2.1.md index 66f06fedf97b..a7388e8bf201 100644 --- a/aspnetcore/signalr/configuration/includes/configuration2.1.md +++ b/aspnetcore/signalr/configuration/includes/configuration2.1.md @@ -124,6 +124,44 @@ The WebSocket transport has additional options that can be configured using the Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. +### Configure the hub URL endpoint + +To set a static hub URL endpoint, pass the URL to . The following example sets the URL to `https://contoso.com/chathub`: + +```csharp +var connection = new HubConnectionBuilder() + .WithUrl("https://contoso.com/chathub") + .Build(); +``` + +To configure the endpoint in code, use to build the URL and pass it to . The following example configures a custom hub URL for production on a target IIS server: + +```csharp +#if DEBUG + // IIS Express development environment (development build) + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .WithAutomaticReconnect() + .Build(); +#else + // IIS server production environment (release build) + var hostName = Environment.MachineName; + var addresses = Dns.GetHostAddresses(hostName); + var ipv4 = addresses.FirstOrDefault(addresses => + addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + var uri = + new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) + { + Scheme = "http", Port = 80, Host = ipv4 + }.ToString(); + + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri(uri)) + .WithAutomaticReconnect() + .Build(); +#endif +``` + ### Configure logging Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information. diff --git a/aspnetcore/signalr/configuration/includes/configuration2.2.md b/aspnetcore/signalr/configuration/includes/configuration2.2.md index 583a6ed828e9..b97982a28d14 100644 --- a/aspnetcore/signalr/configuration/includes/configuration2.2.md +++ b/aspnetcore/signalr/configuration/includes/configuration2.2.md @@ -125,6 +125,44 @@ The WebSocket transport has additional options that can be configured using the Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. +### Configure the hub URL endpoint + +To set a static hub URL endpoint, pass the URL to . The following example sets the URL to `https://contoso.com/chathub`: + +```csharp +var connection = new HubConnectionBuilder() + .WithUrl("https://contoso.com/chathub") + .Build(); +``` + +To configure the endpoint in code, use to build the URL and pass it to . The following example configures a custom hub URL for production on a target IIS server: + +```csharp +#if DEBUG + // IIS Express development environment (development build) + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .WithAutomaticReconnect() + .Build(); +#else + // IIS server production environment (release build) + var hostName = Environment.MachineName; + var addresses = Dns.GetHostAddresses(hostName); + var ipv4 = addresses.FirstOrDefault(addresses => + addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + var uri = + new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) + { + Scheme = "http", Port = 80, Host = ipv4 + }.ToString(); + + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri(uri)) + .WithAutomaticReconnect() + .Build(); +#endif +``` + ### Configure logging Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information. diff --git a/aspnetcore/signalr/configuration/includes/configuration3.1.md b/aspnetcore/signalr/configuration/includes/configuration3.1.md index c2f293ced9e0..d98cad39df2e 100644 --- a/aspnetcore/signalr/configuration/includes/configuration3.1.md +++ b/aspnetcore/signalr/configuration/includes/configuration3.1.md @@ -130,6 +130,44 @@ The WebSocket transport has additional options that can be configured using the Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. +### Configure the hub URL endpoint + +To set a static hub URL endpoint, pass the URL to . The following example sets the URL to `https://contoso.com/chathub`: + +```csharp +var connection = new HubConnectionBuilder() + .WithUrl("https://contoso.com/chathub") + .Build(); +``` + +To configure the endpoint in code, use to build the URL and pass it to . The following example configures a custom hub URL for production on a target IIS server: + +```csharp +#if DEBUG + // IIS Express development environment (development build) + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .WithAutomaticReconnect() + .Build(); +#else + // IIS server production environment (release build) + var hostName = Environment.MachineName; + var addresses = Dns.GetHostAddresses(hostName); + var ipv4 = addresses.FirstOrDefault(addresses => + addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + var uri = + new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) + { + Scheme = "http", Port = 80, Host = ipv4 + }.ToString(); + + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri(uri)) + .WithAutomaticReconnect() + .Build(); +#endif +``` + ### Configure logging Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information. diff --git a/aspnetcore/signalr/configuration/includes/configuration3.md b/aspnetcore/signalr/configuration/includes/configuration3.md index 5bdb5bb00597..2127201474ef 100644 --- a/aspnetcore/signalr/configuration/includes/configuration3.md +++ b/aspnetcore/signalr/configuration/includes/configuration3.md @@ -129,6 +129,44 @@ The WebSocket transport has additional options that can be configured using the Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. +### Configure the hub URL endpoint + +To set a static hub URL endpoint, pass the URL to . The following example sets the URL to `https://contoso.com/chathub`: + +```csharp +var connection = new HubConnectionBuilder() + .WithUrl("https://contoso.com/chathub") + .Build(); +``` + +To configure the endpoint in code, use to build the URL and pass it to . The following example configures a custom hub URL for production on a target IIS server: + +```csharp +#if DEBUG + // IIS Express development environment (development build) + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .WithAutomaticReconnect() + .Build(); +#else + // IIS server production environment (release build) + var hostName = Environment.MachineName; + var addresses = Dns.GetHostAddresses(hostName); + var ipv4 = addresses.FirstOrDefault(addresses => + addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + var uri = + new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) + { + Scheme = "http", Port = 80, Host = ipv4 + }.ToString(); + + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri(uri)) + .WithAutomaticReconnect() + .Build(); +#endif +``` + ### Configure logging Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information. diff --git a/aspnetcore/signalr/configuration/includes/configuration5.md b/aspnetcore/signalr/configuration/includes/configuration5.md index 25336f2b2a2e..7f4f9b0f99e3 100644 --- a/aspnetcore/signalr/configuration/includes/configuration5.md +++ b/aspnetcore/signalr/configuration/includes/configuration5.md @@ -131,6 +131,44 @@ The WebSocket transport has additional options that can be configured using the Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. +### Configure the hub URL endpoint + +To set a static hub URL endpoint, pass the URL to . The following example sets the URL to `https://contoso.com/chathub`: + +```csharp +var connection = new HubConnectionBuilder() + .WithUrl("https://contoso.com/chathub") + .Build(); +``` + +To configure the endpoint in code, use to build the URL and pass it to . The following example configures a custom hub URL for production on a target IIS server: + +```csharp +#if DEBUG + // IIS Express development environment (development build) + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .WithAutomaticReconnect() + .Build(); +#else + // IIS server production environment (release build) + var hostName = Environment.MachineName; + var addresses = Dns.GetHostAddresses(hostName); + var ipv4 = addresses.FirstOrDefault(addresses => + addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + var uri = + new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) + { + Scheme = "http", Port = 80, Host = ipv4 + }.ToString(); + + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri(uri)) + .WithAutomaticReconnect() + .Build(); +#endif +``` + ### Configure logging Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information. diff --git a/aspnetcore/signalr/configuration/includes/configuration6.md b/aspnetcore/signalr/configuration/includes/configuration6.md index b794b551d1f5..f03b58c92027 100644 --- a/aspnetcore/signalr/configuration/includes/configuration6.md +++ b/aspnetcore/signalr/configuration/includes/configuration6.md @@ -114,6 +114,44 @@ The WebSocket transport has additional options that can be configured using the Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. +### Configure the hub URL endpoint + +To set a static hub URL endpoint, pass the URL to . The following example sets the URL to `https://contoso.com/chathub`: + +```csharp +var connection = new HubConnectionBuilder() + .WithUrl("https://contoso.com/chathub") + .Build(); +``` + +To configure the endpoint in code, use to build the URL and pass it to . The following example configures a custom hub URL for production on a target IIS server: + +```csharp +#if DEBUG + // IIS Express development environment (development build) + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .WithAutomaticReconnect() + .Build(); +#else + // IIS server production environment (release build) + var hostName = Environment.MachineName; + var addresses = Dns.GetHostAddresses(hostName); + var ipv4 = addresses.FirstOrDefault(addresses => + addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + var uri = + new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) + { + Scheme = "http", Port = 80, Host = ipv4 + }.ToString(); + + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri(uri)) + .WithAutomaticReconnect() + .Build(); +#endif +``` + ### Configure logging Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information. diff --git a/aspnetcore/signalr/configuration/includes/configuration7.md b/aspnetcore/signalr/configuration/includes/configuration7.md index b76087484bf9..12b61b1d5e1b 100644 --- a/aspnetcore/signalr/configuration/includes/configuration7.md +++ b/aspnetcore/signalr/configuration/includes/configuration7.md @@ -115,6 +115,44 @@ The WebSocket transport has additional options that can be configured using the Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. +### Configure the hub URL endpoint + +To set a static hub URL endpoint, pass the URL to . The following example sets the URL to `https://contoso.com/chathub`: + +```csharp +var connection = new HubConnectionBuilder() + .WithUrl("https://contoso.com/chathub") + .Build(); +``` + +To configure the endpoint in code, use to build the URL and pass it to . The following example configures a custom hub URL for production on a target IIS server: + +```csharp +#if DEBUG + // IIS Express development environment (development build) + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .WithAutomaticReconnect() + .Build(); +#else + // IIS server production environment (release build) + var hostName = Environment.MachineName; + var addresses = Dns.GetHostAddresses(hostName); + var ipv4 = addresses.FirstOrDefault(addresses => + addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); + var uri = + new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) + { + Scheme = "http", Port = 80, Host = ipv4 + }.ToString(); + + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri(uri)) + .WithAutomaticReconnect() + .Build(); +#endif +``` + ### Configure logging Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information.