views:

290

answers:

3

All, my situation is that I have the basic route, plus some other simple routes:

routes.MapRoute(
                "Default",                                              
                "{controller}/{action}/{id}",                           
                new { controller = "Home", action = "Index", id = 1}
            );

So the following url works: http://somesite.com/tags/index/1

However, some of my index pages take url parameters in the following fashion:

http://somesite.com/tags/index/1?when=lastmonth

How do I use Html.RouteLink to link to this?

You can't add '?' to routes in the global asax file like this:

routes.MapRoute("TagsWhen", "Tags/index/{id}?when={when}",
      new {controller = "Tags", action = "Index", id = "", when = ""});

If this route worked I could link to it using:

Html.RouteLink(string.Format("{0} ", link.Rating), "LinksWhen", 
               new {id=link.ReferenceId, when=Model.When})

but it doesn't! So I'm not sure how I would use a Html.RouteLink to generate http://somesite.com/tags/index/1?when=lastmonth

A: 

I don't have a computer able to test this, but off the top of my head

<%= Html.ActionLink("Link text", "Index", "Home", new { id = 1, when = "lastmonth" } %>

You don't need to specify optional parameters in your routes in the global.asax.cs file. Anything that doesn't match gets chucked into the query string by default.

Kevin Pang
but wouldn't that generate an improper url?
Matthew Rathbone
Just tried this, and it generates:http://somesite.com/tags/index?when=lastmonthand doesn't add the id...
Matthew Rathbone
A: 

Just found the solution myself. You can just do a regular Html.RouteLink and any object properties you don't have mapped to the url in global.asax it adds as a url parameter.

So using this route:

    routes.MapRoute(
        "Links",                                              
        "Links/details/{id}",                           
        new { controller = "Links", action = "Details", id = ""} defaults
    );

and this routelink:

Html.RouteLink("Link Text", "Links", 
        new {id=link.ReferenceId, when=Model.When })

generates the correct url:

http://localhost:2535/Links/details/1?when=onemonth

Matthew Rathbone
You should still be able to base this off an ActionLink. I don't know why Matthew's solution didn't work? I do the same thing as him using ActionLink where I have a url: .../delete/124?otherId=5
GONeale
Plus if you use a RouteLink, your adding another dependency, the "Links" route name must always exist and you're tying the name to a ref. in your view (if you ever do change the name you'll hit an exception too). Because you have a standard route though, an `ActionLink` would be fine and then you don't need code for it's own separate named route.
GONeale
I'm curious. Why would you want to create an 'ugly' url when you could have a pretty one?!
Dan Atkinson
A: 

Matthew's approach probably didn't work because he needed another null parameter on the end, otherwise it passes the route values as html attributes. :)

<%= Html.ActionLink("Link text", "Index", "Home", new { id = 1, when = "lastmonth" }, null) %>

That last MapRoute() you came up with should work fine against it.

GONeale