M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Domain Based Language Picking

In previous tutorials we have looked at multi-language implementation and we learned how to administer languages. In this tutorial we will see how we can get a language based on the website domain. This will help us to automatically set the current language for our website rather than the user selecting it.

For this purpose, we have added a new property "AssociatedDomain" in Language entity type, which will be used to specify a domain name for each language, as shown below.

In order to get the language based on current domain, we need to create a property named "CurrentLanguage" in "App.Context" class available under "App_Code" folder in website project, which should check the domain from current "HttpRquest" object and then finds and returns the appropriate language, as shown below:

Because we marked "AssociatedDomain" property as unique so we can call the "FindByAssociatedDomain()" method generated by M# to find the language based on the current domain or simply return default language if no language is associated with the current domain."CurrentLanguage" property can now be used to set the website language automatically.