0

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'?

neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43
  • 3
    Perpend a `/`. But your should always use `@Url.Action()` or `@Html.ActionLink()` to generate the correct url. –  Jun 14 '16 at 07:21
  • Is it possible for you to answer this question and also tell me how exactly to use `@Html.ActionLink()` in this scenario? – neda Derakhshesh Jun 14 '16 at 07:28
  • I don't know how to thank you . thanks . – neda Derakhshesh Jun 14 '16 at 07:32
  • 1
    Always try your link in a browser directly (regardless of how it is generated). If you get a 404, it's a route issue. In this case, your route is "Itinerary/tours/" but your link is "tours/Itinerary", so won't hit your route and will give you a 404. If you'd used `@Html.ActionLink` or `@Html.RouteLink` (or even `@Url.Action`) then this would not have happened. I'll leave details to Stephen's answer. – freedomn-m Jun 14 '16 at 07:35
  • 1
    Your current path+page is 'Russia/tours' you've requested 'Russia/xxx' so it removes the current page from the path to give you 'Russia' and then appends your new path to give 'Russia/Russia/xxx' – freedomn-m Jun 14 '16 at 07:38
  • @freedomn-m thanks . Now I Understand why it happens .I dont know how to thank you. By your recommends it's better to use action link always. – neda Derakhshesh Jun 14 '16 at 07:50

1 Answers1

4

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 })
  • Yes It works and really really thank you for your help and knowledge. – neda Derakhshesh Jun 14 '16 at 07:53
  • 1
    Just a note regarding paths with "/xxx" - the root of your website may not always be at the root of the webserver (esp if you are using IIS applications). That's why MVC Razor views use "~/images..." etc. If you hardcode a path to "/xxx" and your site gets moved/deployed to "/mysite", it should then be "/mysite/xxx" and the hardcoded path will stop working. The helpers (as detailed here) handle this for you. Always use the helpers. – freedomn-m Jun 14 '16 at 09:11