Skip to content

NDLRSIG001: HubPath must be a constant

Cause

The hubPath parameter of HubPathAttribute is not a compile-time constant expression.

Rule Description

SignalR hub paths must be known at compile time for proper route registration and AOT compatibility. Using non-constant values (like static fields or method calls) prevents the source generator from properly analyzing and generating the hub registration code.

Example

Incorrect

public static class HubPaths
{
    public static string ChatHub = "/chat"; // Not const!
}

[HubPath(HubPaths.ChatHub, typeof(ChatHub))] // NDLRSIG001
public class ChatHubRegistration : ISignalRHub { }

Correct

public static class HubPaths
{
    public const string ChatHub = "/chat"; // const is OK
}

[HubPath(HubPaths.ChatHub, typeof(ChatHub))]
public class ChatHubRegistration : ISignalRHub { }

// Or use a string literal directly
[HubPath("/chat", typeof(ChatHub))]
public class ChatHubRegistration : ISignalRHub { }

How to Fix

  1. Change the hub path to a const string field
  2. Use a string literal directly in the attribute
  3. Use nameof() if deriving the path from a type name

When to Suppress

This warning should not be suppressed. Non-constant hub paths will cause compilation errors in the C# compiler itself (CS0182).

See Also