1

Say I have a ASP.Net MVC 4 web application. In this app I have 2 controllers "Product" and "Support".

This app is a language-aware web app, so when a user navigates to http://server/product/index, I'd like to automatically redirect to http://server/en-us/product/index

Same thing for support: From http://server/support/ to http://server/en-us/support/

I already have some logic to determine the user's language based on the browser, I'm just not sure the best way to achieve that thing explained above.

Just to clarify, I already a default route with language

private const string languagePartPattern = @"(\w{2})|(\w{2}-\w{2,4})";

routes.MapRoute(
    name: "DefaultLang",
    url: "{lang}/{controller}/{action}/{id}",
    constraints: new { lang = languagePartPattern },   //For example: en or en-US
    defaults: new { lang = SettingsHelper.DefaultLanguage, controller = "EntryPoint", action = "Index", id = UrlParameter.Optional }
);

If I navigate to http://server/product/, it does not automatically redirect to http://server/en-us/product/. However, it works fine if I navigate to http://server/en-us/product/ directly

Julien Pierre
  • 467
  • 1
  • 6
  • 29
  • There are _tons_ of articles, blogs and SO questions about this topic. Use the Google (or Bing ;-). – Uwe Keim Jun 01 '16 at 05:53
  • 3
    E.g. [this](http://stackoverflow.com/questions/15006572/mvc-make-url-language-specific-via-routes) or [this](http://stackoverflow.com/questions/1560796/set-culture-in-an-asp-net-mvc-app) or [this](http://stackoverflow.com/a/21125230/107625). – Uwe Keim Jun 01 '16 at 05:54
  • 3
    I did search -> I Bing'ed ;-) and implemented a default route with url: "{lang}/{controller}/{action}/{id}", but if I navigate to http://server/product/ it does not redirect to http://server/en-us/product/. It works fine if I navigate directly to http://server/en-us/product/ though. – Julien Pierre Jun 01 '16 at 06:15
  • 1
    See [Language-specific Default URL using MVC's Attribute Routing and RouteLocalization.mvc](http://stackoverflow.com/questions/37239919/language-specific-default-url-using-mvcs-attribute-routing-and-routelocalizatio) for an example that includes redirecting to the current culture based on the user's browser settings (or any firewalls that happen to change the header - always give your users a way to select culture!!) – NightOwl888 Jun 01 '16 at 06:42
  • See this [post](http://stackoverflow.com/a/32839796/1504480) – Bon Macalindong Jun 01 '16 at 06:56
  • NightOwl888 > This is exactly what I needed, thanks! – Julien Pierre Jun 01 '16 at 07:01
  • This is the answer: http://stackoverflow.com/a/37243835/764205 – Julien Pierre Jun 01 '16 at 07:02

0 Answers0