2

我正在尝试掌握 MVC 路由系统,但它没有像我预期的那样工作。

假设我定义了这条路线:

context.MapRoute(
        "NewCertificates",
        "NewQuote/GetCertificate/{date}/{id}",
        new { area = "NewQuote", controller = "Quote", action = "GetCertificate" },
        new[] { "Acme.Areas.NewQuote.Controllers" }
    );

在我看来这个动作链接:

@Html.ActionLink("Click Here", "GetCertificate", new { controller = "Quote", area = "NewQuote", date = "20121219", id = "acme" })

我希望生成的 URL 看起来像:

http://localhost:50582/NewQuote/GetCertificate/20121219/acme

但相反,我得到:

http://localhost:50582/NewQuote/GetCertificate/acme?date=20121219

谁能告诉我为什么会这样?

编辑以显示上述路线之前的路线:

        context.MapRoute(
                "NewQuoteValidation",
                "NewQuote/Validation/{action}/{id}",
                new { area = "NewQuote", controller = "Validation", action = "IsImeiAvailable", id = UrlParameter.Optional },
                new[] { "Acme.Areas.NewQuote.Controllers" }
            );

        context.MapRoute(
                "NewAjax",
                "NewQuote/Ajax/{action}/{id}",
                new { area = "NewQuote", controller = "Ajax", action = "Index", id = UrlParameter.Optional },
                new[] { "Acme.Areas.NewQuote.Controllers" }
            );

        context.MapRoute(
                "NewQuote",
                "NewQuote/{action}/{id}",
                new { area = "NewQuote", controller = "Quote", action = "Select", id = UrlParameter.Optional },
                new[] { "Acme.Areas.NewQuote.Controllers" }
            );
4

2 回答 2

4

另一个先前的路由定义很可能正在声明该ActionLink定义的所有权。您可能想尝试:

  1. 将“NewCertificates”路由定义放在列表的首位或更高的位置。
  2. 根据路由显式构造链接,使用Html.RouteLink

@Html.RouteLink("Click Here", 
      // route name
    "NewCertificates", 
      // route attributes
    new { controller = "Quote", area = "NewQuote", date = "20121219", id = "acme" })
于 2012-12-19T20:36:47.817 回答
0

试试这个来调用你的路线:

@Html.RouteLink("点击这里", "NewCertificates", new { controller = "Quote", area = "NewQuote", id = "acme" })

于 2012-12-20T09:13:24.763 回答