MVC noob - change part of the URL in the link

I have a site that supports localization. I would like to be able to switch between English and French.

Let's say the user is currently located at: http://www.mysite.com/ en / Home

I would like to redirect to: http://www.mysite.com/ fr / Home

If the user clicks on the "French" link, how to change the URL to "fr", but does not change the "Home" part of the URL (basically I want to keep the user's current location).

Hope my question makes sense! I probably missed something very basic?

EDIT: view of the solution found.

<%= Html.ActionLink("Français", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { culture = "fr" }, null)%> <%= Html.ActionLink("English", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { culture = "en" }, null)%> 

This supports the action / controller of the current URL. Maybe there is a cleaner solution?

+4
source share
3 answers

In your Global.asax application

  routes.MapRoute( name: "LocalizedRoute", url: "{language}/{controller}/{action}/{id}", defaults: new { language= "fr", controller = "Home", action = "Index", id = UrlParameter.Optional }, constraints: new { language= @"[az]{2}" }); 

And you can have access to the language with the language of variables in the controller

To create a link:

 <%= Html.ActionLink("French", ViewContext.RouteData.Values["action"], new { language = "fr" }) %> 

And you can create a base class controller with this property:

 public string Language { get { return this.Routedata["language"]; } } 
+2
source

I made an extension for RouteValueDictionary to solve this problem:

 public static RouteValueDictionary SetValue(this RouteValueDictionary dictionary, string key, object value) { RouteValueDictionary rvd = new RouteValueDictionary(dictionary); rvd[key] = value; return rvd; } 

Now you can use this as described in the html template:

 <div id="head"> @Html.RouteLink("DE", ViewContext.RouteData.Values.SetValue("language", "de")) | @Html.RouteLink("FR", ViewContext.RouteData.Values.SetValue("language", "fr")) | @Html.RouteLink("IT", ViewContext.RouteData.Values.SetValue("language", "it")) | @Html.RouteLink("EN", ViewContext.RouteData.Values.SetValue("language", "en")) </div> 

The extension does not modify the RoutData.Values ​​collection, it makes a copy, and you can cancel the required value.

+1
source

I suggest making a url in this format:

 http://www.mysite.com/Home/en 

Then you can do these simple steps:

 <%= Html.ActionLink("Francais", "Home", "MyController", new { id = "fr" }), null %> <%= Html.ActionLink("English", "Home", "MyController", new { id = "en" }), null %> 

Then in your controller there is an action:

 public ActionResult Home(string id) { if(id == "en"){ // do something } else if(id == "fr") { // do something else } return View(); } 

Route Global.asax

 routes.MapRoute( "HomeLanguageRoute", // Route name "MyController/Home/{id}", // URL with parameters new { controller = "MyController", action = "Home", id = "" } // Parameter defaults ); 
0
source

Source: https://habr.com/ru/post/1306408/


All Articles