4

I need to get the value of language parameter i tried this code

if(HttpContext.Current.Request.RequestContext.RouteData.Values["language"] == null)
 {
    HttpContext.Current.Request.RequestContext.RouteData.Values["language"] =   "en-US";

}

the above code makes the url like the following which is very good

http://localhost:25576/en-US/Home

the problem is when the user enters http://localhost:25576/Home (without en-US) the value of HttpContext.Current.Request.RequestContext.RouteData.Values["language"] becomes "Home"

My question is how to get the real value of language parameter if the user removes en-US or if the user enters http://localhost:25576/Home

RouteConfig

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

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

              }
                  );
         }
Alex
  • 195
  • 3
  • 3
  • 12

2 Answers2

3

You can create a new ActionFilterAttribute for that purpose:

public class LocalizationAttribute : ActionFilterAttribute
{
    private readonly string _defaultLanguage = "en-US";

    public LocalizationAttribute(string defaultLanguage = null)
    {
        this._defaultLanguage = defaultLanguage ?? this._defaultLanguage;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var language = filterContext.RouteData.Values["language"] as string ?? this._defaultLanguage;

        if (language != this._defaultLanguage)
        {
            try
            {
                Thread.CurrentThread.CurrentCulture =
                    Thread.CurrentThread.CurrentUICulture =
                        CultureInfo.CreateSpecificCulture(language);
            }
            catch
            {
                throw new NotSupportedException($"Invalid language code '{language}'.");
            }
        }
    }
}

You also need to register that ActionFilter as part of the GlobalFilter.

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new LocalizationAttribute("en-US"), 0);
    }
}
msmolcic
  • 6,407
  • 8
  • 32
  • 56
  • Thank you for your help and effort :) I tried your code and still get The resource cannot be found message when i enter http://localhost:25576/Home/Contact instead of http://localhost:25576/en-US/Home/Contact – Alex Jul 15 '17 at 13:19
  • @Alex Have you tried creating a `LocalizationAttribute`? Also, your localized route should be registered after the default route. – msmolcic Jul 15 '17 at 13:20
  • Yes I've tried and still facing the same problem – Alex Jul 15 '17 at 14:36
0

The token of "language" is "null" or "empty" in your codes, if a user's request ignored this. So you MUST assign it as a default value, such as "en-US".

Take this as an example:

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

            routes.MapRoute(
              name: "DefaultLocalized",
              url: "{language}/{controller}/{action}/{id}",
              defaults: new
              {
                  controller = "Home",
                  action = "Index",
                  id = UrlParameter.Optional,
                  language = "en-US"

              }
                  );
         }

And please remove the language assignment snippet of codes you've shown to us above, it can be TOTALLY done through the default route config.

xqMogvKW
  • 628
  • 7
  • 17
  • Thank you for your help and effort :) I tried your code and still get The resource cannot be found message when i enter http://localhost:25576/Home/Contact instead of http://localhost:25576/en-US/Home/Contact – Alex Jul 15 '17 at 13:19
  • NightOwl888, Can you please help me – Alex Jul 15 '17 at 14:40
  • 1
    @Alex - The [duplicate question](https://stackoverflow.com/a/32839796) does provide the answer, but it is buried among other stuff the other user was doing wrong that you are not. Scroll down to the section titled **Default Culture** to see how. You just need a 2nd default route *without* the `language` parameter in the URL (but the rest of the URL the same) that sets the `language` route value to your default language. The constraint is also needed if you want your default culture to be generated without a culture in the URL. – NightOwl888 Jul 15 '17 at 16:14