views:

151

answers:

2

I have these 2 routes :

routes.MapRoute("Agenda", ConfigurationManager.AppSettings["eventsUrl"] + "/{year}/{month}", MVC.Events.Index(), new { year = DateTime.Now.Year, month = DateTime.Now.Month });
routes.MapRoute("AgendaDetail", ConfigurationManager.AppSettings["eventsUrl"] + "/{year}/{month}/{day}", MVC.Events.Detail(), new { year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day });

And it work perfectly with this code :

<a href="<%= Url.Action(MVC.Events.Detail(Model.EventsModel.PreviousDay.Year, Model.EventsModel.PreviousDay.Month, Model.EventsModel.PreviousDay.Day))%>" title="<%= Model.EventsModel.PreviousDay.ToShortDateString() %>"><img src="<%= Links.Content.images.contenu.calendrier.grand.mois_precedent_png %>" alt="événement précédent" /></a>

Except when I get to do the link to today, if it's today, il will point only to www.myurl.com/agenda, witch is the value of CnfigurationManager.AppSettings["eventsUrl"]. What am I doing wrong? It's like if it's today, it point bak to the default agenda...

Thanks for the help!

+1  A: 

Actually, both your routes and the Url.Action() call are working exactly as you would expect them to: when the route data is the same as the default data, it is omitted from the URL. And since you give DateTime.Now.Day etc as default values, when linking to today's agenda it will not include any date values.

However, this will still behave as you want it to. If you click the link to today's agenda, you will in fact get today's agenda shown - only not in the URL.

Tomas Lycken
Ok now I understand what's happening, since my url without the params is the same for the agenda and the details it goes to the first one witch is monthly view. My url are www.myurl.com/agenda/year/month and www.myurl.com/agenda/year/month/day so witouht the params it's both www.myurl.com/agenda/ how can I handle that? Specifying old date as default params?
VinnyG
@Vinny: Well, you will always have some date or other where this happens. Either you can let there be *no* default values, and show a "not found" page when none are supplied, or you could just decide which view you want as default (per month or per day) and place the corresponding route first. Now that I think about it, the second option would probably only work if you want the day view as default, since otherwise `{month}/{day}` would be matched for the `month` parameter...
Tomas Lycken
Thanks Thomas, I try to change the order of my route but it's not working like I want since when I click on my link that goes to my monthly calendar, it goes ti the day view. But by removing the default parameter for the dayly view it works perfectly!
VinnyG
A: 

One thing to look into is whether you really need to have all these default values in your routes, as they are the source of the issue. e.g. what happens if you simply remove the 'day = DateTime.Now.Day' default value from the second route?

You need to decide what you want your URLs to do, e.g.

www.myurl.com/agenda/2010/04

Do you want that to show the agenda for 4/2010, or do you want it to show today's AgendaDetail?

David Ebbo
I want both, if it's monthly view I show a big calendar of the month with every events. And if it's dayly, I show details of the day's events. So I need both Url.
VinnyG
Did you try my suggestion to remove the day default parameter?
David Ebbo