views:

32

answers:

1

I'm trying to write out animals from a database. All the samples I see for creating View pages have hard-coded "nodes" in the URLs. I can get my first view to work but my second one doesn't write out the correct URL to go to the third one. So, for example, I want my URLs to be:

 /animals/
 /animals/canines/
 /animals/canines/schnauzer

I have the default route setup:

> routes.MapRoute( _
>             "Default", _
>             "{controller}/{action}/{id}", _
>             New With {.controller = "Home", .action = "Index", .id =
> UrlParameter.Optional} _
>         )

The first URL works great and I can list the animal groups on the first one along with the correct links to the second URLs. I do that as follows:

for each animal in model
     Html.ActionLink(animal.animal_name,animal.animal_name.Trim) 
next

I also can get the canine groups written out on the second (so I know my model is working), however the URL.Action on that second "canines" page loses the "canines" in the URL so it renders as:

/animals/schnauzer

I've tried every way I can think of to write out the link including:

<a href="<%= url.action(canine.dog_name.Trim)) %>">
<a href="<%= Url.Content("~" & url.action(canine.dog_name.Trim)) %>">

and a few others that aren't worth showing. ;-D

... I'm guessing I'm missing something in a route path but what is it? It can't be that I have to know the path I'm at on every page in order to write out the URL - can it?

Thanks in advance for your help!

A: 

The route youve configured looks for three components, a controller, action and id. In your case /animals/schnauzer, you're looking for more than a single route element as far as HTTP routing (and link generation), is concerned. It sees /animals/canines as two different values. If you're looking to allow a route like http://yoursite/Family/Species, specify it as such:

routes.mapRoute("familyspecies", "animals/{family}/{species}", new { controller = "Home", action = "Animals", Species = UrlParameter.Optional});

and pass family and species in to your actions

public ActionResult Animals(string family, string species){...}

OR Use wildcard routes to direct everything after the action to the id

routes.MapRoute("{controller}/{action}/{*id}"); 
Rob Rodi