我是MVC ASP.net 新手
我的路由配置在这里
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>
当我点击这个时,页面将链接到这个网址:/Russia/Russia/tours/Itinerary/1
错误 404 找不到 http。
你知道为什么我有两个俄罗斯以及如何修复它以在"TourDetailsController"中使用"TourId"链接吗?
/
(正斜杠)附加到href
值 - 即使其href="/Russia/tours/Itinerary/1"
,但您应该始终使用 UrlHelper
或 HtmlHelper
方法来生成链接
使用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 })