@ Url.Action附加?Length = 2 [英] @Url.Action getting ?Length=2 appended

查看:78
本文介绍了@ 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:

http://localhost:65391/Terms/

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

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

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

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

Also, oddly, if I manually type in

http://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:

http://localhost:65391/Terms/de

转到此处查看实际站点:

Go here to see the actual site:

http://inrix.com/traffic/terms

推荐答案

您正在使用

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将使用已经给定的路由值.这是您在http://localhost:65391/Terms/de案例中所看到的.因此,英文链接变成了http://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 http://localhost:65391/Terms/de case. So the English link became http://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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆