0

我是 ASP.net MVC 的新手

我的路线配置在这里

    routes.MapRoute(
          name: "ItineraryRoute",
          url: "{country}/Itinerary/tours/{TourId}",
          defaults: new { controller = "TourDetails", action = "Index" }
      );

        routes.MapRoute(
           name: "TourRoute",
           url: "{country}/tours",
           defaults: new { controller = "Tour", action = "Index" }
       );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

在页面/Russia/tours 中,我有一个链接,这里是代码行:

<a href="@ViewBag.Country/tours/Itinerary/@tour.Id">Day's By Detail's....</a>

当我点击这个时,页面将链接到这个 Url:/Russia/Russia/tours/Itinerary/1

找不到错误 404 http。

您知道为什么我有两个俄罗斯以及如何修复它以将“TourDetailsController”与“TourId”链接起来吗?

4

1 回答 1

4

您需要/在值上附加一个(正斜杠)href- 即它的href="/Russia/tours/Itinerary/1",但您应该始终使用UrlHelperorHtmlHelper方法来生成链接

使用Url.Action()

<a href="@Url.Action("Index", "TourDetails", new { country = ViewBag.Country, tourID = tour.Id })">Day's By Detail's....</a>

使用Url.RouteUrl()

<a href="@Url.RouteUrl("ItineraryRoute", new { country = ViewBag.Country, tourID = tour.Id })">Day's By Detail's....</a>

使用Html.Action()

@Html.ActionLink("Day's By Detail's....", "Index", "TourDetails", new { country = ViewBag.Country, tourID = tour.Id }, null)

使用Html.RouteLink()

@Html.RouteLink("Day's By Detail's....", "ItineraryRoute", new { country = ViewBag.Country, tourID = tour.Id })
于 2016-06-14T07:49:32.947 回答