Solved

.NET MVC (5) Localization based on URL

Posted on 2014-12-22
3
503 Views
Last Modified: 2014-12-22
I have an application in .NET MVC5 with 2 supported languages, English as default. I currently use cookies to switch between languages but would like to change the application to make use of URL's for specifying the language. I have found a solution that works, but the default language, English, must currently appear in the URL  ".../en/...".

How can I make use of URL for this purpose, while having the default (English) work without a culture (language) specified in the URL? English would be the default, when no culture (language) is specified in the URL and the second language would be accessible by including the culture (language) in the URL.
0
Comment
Question by:lynnwalker
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40514115
How are you implementing the URL based language selection currently? If you are using RouteConfig have a look at this tutorial

http://www.codeproject.com/Articles/624180/Routing-Basics-in-ASP-NET-Web-API
0
 

Author Comment

by:lynnwalker
ID: 40514237
I am using RouteConfig, but in an MVC application, not Web API. Here is the current RouteConfig:

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(name: "DefaultWithCulture"
                           , url: "{culture}/{controller}/{action}/{id}"
                           , defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                           , constraints: new { culture = "[a-z]{2}" }
            );

            routes.MapRoute(name: "Default"
                           , url: "{controller}/{action}/{id}"
                           , defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40514250
In your default MapRoute set the culture you want as the default

routes.MapRoute(name: "Default"
                           , url: "{controller}/{action}/{id}"
                           , defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syntax error 9 45
Error when loading the database 16 57
SSL https .net web site force redirect 3 37
Crystal reports vb.net 2 16
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now