简体   繁体   中英

problem with routing/T4MVC Url.Action()

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!

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.

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. eg 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, eg

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM