Search code examples
asp.netasp.net-mvcasp.net-mvc-5routeconfig

Asp.net MVC 5 Routing


I'm new in ASP.net MVC

My Route Config is here

    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 }
        );

In page /Russia/tours I have a link here is the code line:

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

When I click on this , page would be link to this Url : /Russia/Russia/tours/Itinerary/1

With Error 404 http not Found.

Do you have any idea why I have two Russia and how to fix it to link in 'TourDetailsController' with 'TourId'?


Solution

  • You would need perpend a / (forward slash) to the href value - i.e. so that its href="/Russia/tours/Itinerary/1", but you should always use the UrlHelper or HtmlHelper methods to generate you links

    Using Url.Action()

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

    Using Url.RouteUrl()

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

    Using Html.Action()

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

    Using Html.RouteLink()

    @Html.RouteLink("Day's By Detail's....", "ItineraryRoute", new { country = ViewBag.Country, tourID = tour.Id })