代码之家  ›  专栏  ›  技术社区  ›  GONeale

RedirectToAction返回URL格式冗长/不正确

  •  1
  • GONeale  · 技术社区  · 15 年前

    为什么在ASP.NET MVC中我使用

    return this.RedirectToAction("Index", "Page", new { pageKey = "test/ho/hum"})
    

    return this.RedirectToAction<PageController>(c => c.Index("test/ho/hum"))
    

    将我的返回URL格式化为:
    http://localhost:8882/?pageKey=test%2Fho%2Fhum
    而不是:
    http://localhost:8882/test/ho/hum

    {*pageKey} 指向PageController上的索引()。

    你知道为什么吗 RedirectToAction 你喜欢吗?

    2 回复  |  直到 15 年前
        1
  •  0
  •   Wei Ma    15 年前

    在Global.asax.cs文件中,尝试添加以下路由信息:

      routes.MapRoute(
                "my customized routing",
                "Index/Page/{*MyPageKey}",
                new { controller = "Index", Action = "List", MyPageKey= "" }
            );
    

    你可能会得到你所期望的。但是,这可能会在应用程序中产生大量错误。所以要小心使用。

        2
  •  0
  •   Eilon    15 年前

    ASP.NET路由遍历路由列表并找到第一个匹配的路由。在这种情况下,必须在匹配的{*pageKey}路由之前有一个较早的路由。

    RedirectToRoute ,您可以在其中指定路由的名称,以确保只有该路由可以匹配。

    推荐文章