Welcome to the navigation

Voluptate aliqua, exercitation nostrud quis pariatur, velit excepteur sunt in ut cillum in eu eiusmod in ipsum sit sed veniam, id est consequat, deserunt amet. Ex sunt mollit exercitation ad ut sed incididunt est dolor dolor fugiat commodo irure in cillum quis sit in cupidatat et adipisicing velit deserunt lorem

Yeah, this will be replaced... But please enjoy the search!

Getting the client locale using ASP.NET Web API

I think it is very neat to automatically help the user fill out some forms, adapt date and time formats and so on. Small things like this keeps visitors satisfied which is rather important! Using ASP.NET Web API for this is simple The query address will be something like http://yoursite/api/locale depending on your routing.

using System.Net.Http;
using System.Web;
using System.Web.Http;
 
namespace Trikks.Controllers.Api
{
    public class LocaleController : ApiController
    {
        public string[] GetLocale()
        {
            return GetClientLocale();
        }
 
        private string[] GetClientLocale(HttpRequestMessage request = null)
        {
            request = request ?? Request;
 
            if (request.Properties.ContainsKey("MS_HttpContext"))
            {
                return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserLanguages;
            }
            else if (HttpContext.Current != null)
            {
                return HttpContext.Current.Request.UserLanguages;
            }
            else
            {
                return null;
            }
        }
    }
}

The returns will look something like this

["sv-SE","sv;q=0.8","en-US;q=0.6","en;q=0.4"]

Store the response in a javascript variable

var locales = ["sv-SE","sv;q=0.8","en-US;q=0.6","en;q=0.4"];
var primaryLocale = locales[0];

primaryLocale will contain the first locale, in this case "sv-SE"

Enjoy

Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author