@ Url.Action receipt? Length = 2 Added

I have this at the top of each of several translations of the Terms of Use page:

<li><a href="@Url.Action("Index", "Terms")">English</a></li> <li><a href="@Url.Action("Index", "Terms", "de")">Deutsch</a></li> <li><a href="@Url.Action("Index", "Terms", "fr")">Français</a></li> <li><a href="@Url.Action("Index", "Terms", "it")">Italiano</a></li> <li><a href="@Url.Action("Index", "Terms", "nl")">Nederlands</a></li> <li><a href="@Url.Action("Index", "Terms", "hu")">Maygar</a></li> <li><a href="@Url.Action("Index", "Terms", "es")">Español</a></li> <li><a href="@Url.Action("Index", "Terms", "zh")">简体中文</a></li> <li><a href="@Url.Action("Index", "Terms", "pt-pt")">European Português</a></li> <li><a href="@Url.Action("Index", "Terms", "pt")">Português</a></li> 

This is an action that should handle clicks:

 public class TermsController : Controller { public ActionResult Index(string id) { switch (id) { case "de": return View("de"); case "fr": return View("fr"); case "it": return View("it"); case "nl": return View("nl"); case "hu": return View("hu"); case "es": return View("es"); case "zh": return View("zh"); case "pt": return View("pt"); case "pt-pt": return View("pt-pt"); default: return View(); } } 

and these are my routes:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Terms", "{controller}/{id}", new { controller = "Terms", action = "Index" } ); routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ); routes.MapRoute( "ThankYou", "{controller}/{action}/{email}/{id}" ); } 

From the main page (for example, in English), the first line (for example, English) looks right:

 http://localhost:65391/Terms/ 

Why do other (i.e., foreign) generated URLs look like this?

 http://localhost:65391/Terms/?Length=2 

It’s also strange if I manually type

 http://localhost:65391/Terms/de 

for example, and go to the "Terms" page in German, then the first hyperlink (for example, back to the "Terms of Use" page) looks like this:

 http://localhost:65391/Terms/de 

Go here to see the actual site:

http://inrix.com/traffic/terms

+6
source share
1 answer

You are using Url.Action overload , which treats the third argument as a routeValues object .

From MSDN:

routeValues
Type: System.Object
An object containing the parameters for the route. Parameters are extracted by reflection by examining the properties of the object. Usually an object created using the object initializer syntax.

So, you passed the strings "de", "fr" as the third argument, so MVC took its properties and made a pair of key values: this is where Length=2 fits, because the string class has one Length property and the value is 2 for your strings .

You can easily fix this by passing an anonymous object wrapping your lines:

 <li><a href="@Url.Action("Index", "Terms" new { id = "" })">English</a></li> <li><a href="@Url.Action("Index", "Terms", new { id = "de" })">Deutsch</a></li> <li><a href="@Url.Action("Index", "Terms", new { id = "fr" })">Français</a></li> ... 

Notes:

  • Your property name for the anonymous id object must match the name of your route id segment and the name of the id controller parameter
  • you need to pass the expicilty new { id = "" } check in the default case, otherwise MVC will use the route values ​​already set. This is what you saw with http://localhost:65391/Terms/de . So the English link became http://localhost:65391/Terms/de because MVC already found the id value in the url that was de and automatically reused it.
  • Last Note is the correct spelling of Magyar, not Maygar
+10
source

All Articles