Search code examples
http-headerssignalrsignalr-hub

Checking for an optional header in a SignalR hub


In a SignalR hub method, Context.Headers provides a INameValueCollection with the request headers. INameValueCollection has just three members:

    string this[string key] { get; }
    string Get(string key);
    IEnumerable<string> GetValues(string key);

Unfortunately, none of them are documented. If you want to get a header but not throw an exception if it doesn't exist, what do you use? I'm guessing Get, but it sure would be nice if the author had bothered to document these details.

One thing I like about the "old" Microsoft was that even if a bit verbose, its documentation covered nearly all the semantics. SignalR was a wonderful, rapid development, but it would be even better if it retained that old-school diligence.

Perhaps I'm missing something. Are the semantics documented somewhere? Or does someone know and care to document them here as a quick and dirty workaround?


Solution

  • Had the same problem and in the end I looked it up in the github source which takes you System.Collections.Specialized.NameValueCollection

    (there are other implementation of INameValueCollection as well but the one linked seems to be used in the request)

    In short:

    string this[string key] { get; }
    string Get(string key);
    

    A String that contains the comma-separated list of values associated with the specified key, if found; otherwise, null.

    IEnumerable<string> GetValues(string key);
    

    A String array that contains the values associated with the specified key from the NameValueCollection, if found; otherwise, null.