ASP.NET MVC: How to get route values

Viewed 11130

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 = ""

              }
                  );
         }
2 Answers
Related