@ Url.Action附加?Length = 2

编程入门 行业动态 更新时间:2024-10-25 02:19:32
本文介绍了@ Url.Action附加?Length = 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在使用条款"页面的几种翻译中,我都将其放在顶部:

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 the action that should handle the 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(); } }

这是我的路线:

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 (i.e., English) Terms page, the first (i.e., English) link looks correct:

localhost:65391/Terms/

为什么其他(即外部)生成的URL看起来像这样?

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

localhost:65391/Terms/?Length=2

奇怪的是,如果我手动输入

Also, oddly, if I manually type in

localhost:65391/Terms/de

例如,然后转到德语的条款"页面,则第一个超链接(即,回到英语条款"页面)如下所示:

for example, and go to the Terms page in German, then the first hyperlink (i.e., back to the English Terms page) looks like this:

localhost:65391/Terms/de

转到此处查看实际站点:

Go here to see the actual site:

inrix/traffic/terms

推荐答案

您正在使用 Url.Action的重载,它将第三个参数视为 routeValues 对象.

You are using an overload of the Url.Action which treats the third argument as the routeValues object.

从MSDN:

routeValues 类型:System.Object 包含参数的对象 一条路线.通过反射通过以下方式检索参数 检查对象的属性.该对象通常是 使用对象初始化程序语法创建的.

routeValues Type: System.Object An object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.

因此,您已将字符串"de", "fr"作为第三个参数传递,因此MVC取得了其属性并创建了键值对:这就是Length=2即将到来的地方,因为string类具有一个属性Length,并且您的字符串的值为2.

So you have passed strings "de", "fr" as the third argument so MVC have taken its properties and made key value pairs: that is where the Length=2 is coming, because the string class has one property Length and the value is 2 for your strings.

您可以通过传递一个包装字符串的匿名对象来轻松解决此问题:

You can fix this easily with passing an anonymous object wrapping your strings:

<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> ...

注意:

  • 您的匿名对象属性名称id应该与您的路由段名称id和控制器参数名称id
  • 相匹配
  • 您需要在默认情况下通过new { id = "" },否则MVC将使用已经给定的路由值.这是您在localhost:65391/Terms/de案例中所看到的.因此,英文链接变成了localhost:65391/Terms/de,因为MVC已在URL中找到了de值,该值就是de,并自动重用了它.
  • 最后请注意,正确的拼写是Magyar而不是Maygar
  • your annonymous object property name id should match your route segment name id and controller parameter name id
  • you need to expicilty pass new { id = "" } in the default case otherwise MVC will use the already given route values. This is what you have seen in the localhost:65391/Terms/de case. So the English link became localhost:65391/Terms/de because MVC already found the id value in the URL which was de and automatically reused it.
  • Last Note the correct spelling is Magyar and not Maygar

更多推荐

@ Url.Action附加?Length = 2

本文发布于:2023-11-03 20:37:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556031.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Url   Action   Length

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!