When I try to request a page for the first time (without any culture specified in url or cookie), the BrowserBestMatch function does not return the expected culture.
My browser (Google Chrome) primarily expects French (fr-FR) but i get the application default culture instead (en-GB).
This happens because in the following code we expect to get a string array from the "Accept-Language" field of the request header with each string representing one and only one language:
string acceptLanguageHeader = null;
if (context.Request.Headers.TryGetValue("Accept-Language", out acceptLanguageHeader) && acceptLanguageHeader != null)
This is not the case. For me, the acceptLanguageHeader variable will contain the following string (There is only one string in the array...):
This is normal behaviour according to the specification:
Accept-Language HTTP header specification
The string needs to be split with the comma character before being passed to the parseLanguage method because this is what this method expects.
To fix this, you should replace the flowing code:
foreach (var language in parseLanguages(acceptLanguageHeader))
with this code:
foreach (var language in parseLanguages(acceptLanguageHeader.Split(',')))