Solved

.NET MVC (5) Localization based on URL

Posted on 2014-12-22
3
497 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

763 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

11 Experts available now in Live!

Get 1:1 Help Now