Routes and ActionLink

Last post 12-12-2007 1:49 AM by Haacked. 3 replies.

Sort Posts:

  • Routes and ActionLink

    12-11-2007, 8:01 PM
    • Loading...
    • c0s
    • Joined on 12-12-2007, 12:52 AM
    • Posts 4

    One of the benefits to using ActionLink is so that I can change the format of my URL and have all my links update automatically, I get that. 

    But if I have "[controller]/[action]/[id]" setup as a route and use

        Html.ActionLink("Something", new { action = "Edit", controller = "Products", id = p.productid }) 

    then I change the route to "[controller]/[action]/[productname]" this won't render correctly because ActionLink doesn't know how to set "productname".

     Am I missing something or am I to add every property to the anonymous object? And if so, what happens if I add properties to the Products class?

     Can I pass in the object and have the ActionLink automatically set fields in the URL to corresponding properties in the object?

     If I can't and I wanted to add this functionality, would I create a RouteHandler or would this be a helper method issue?

     

    Thanks.

    Gabe
     

     

    Filed under: , ,
  • Re: Routes and ActionLink

    12-11-2007, 11:37 PM
    Answer
    • Loading...
    • robconery
    • Joined on 02-23-2005, 10:16 PM
    • Posts 163
    • AspNetTeam

     It's a syntactic thing and you're correct - if you specify "productname" in your Route you need to pass a variable named "productname". So you'd need to update your link thus:

     Html.ActionLink("Link", new{action="Edit", controller="Products",productname="Chang's Ice Tea"})

    But that might not be the best way to do it. You can also rewrite this link:

    Html.ActionLink<ProductController>(x=>x.Edit(p.ProductName))

    The benefit here is that you get a compiler error that complains if you're not passing the right data.

     

  • Re: Routes and ActionLink

    12-12-2007, 12:09 AM
    • Loading...
    • c0s
    • Joined on 12-12-2007, 12:52 AM
    • Posts 4

    So I guess my question now would be: is the URL creation done in the helper or the routehandler?

  • Re: Routes and ActionLink

    12-12-2007, 1:49 AM
    Answer
    • Loading...
    • Haacked
    • Joined on 09-17-2003, 10:43 AM
    • Posts 277
    • AspNetTeam

    It's actually done via the Route object, which the helper uses. The Route instance is part of the url routing set of classes. When you try to generate a route url, we look at which routes you've configured match the parameters you specify and we generate the first one that matches.

    Phil Haack (http://haacked.com/)
    Senior Program Manager, Microsoft

    What wouldn’t you do for a Klondike bar?
Page 1 of 1 (4 items)