开发者

problem with routing/T4MVC Url.Action()

开发者 https://www.devze.com 2022-12-27 14:53 出处:网络
I have these 2 routes : routes.MapRoute(\"Agenda\", ConfigurationManager.AppSettings[\"eventsUrl\"] + \"/{year}/{month}\", MVC.Events.Index(), new { year = DateTime.Now.Year, month = DateTime.Now.Mon

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["even开发者_运维问答tsUrl"]. 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. 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号