路由/T4MVC Url.Action() 问题
我有这两条路线:
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 });
它与此代码完美配合:
<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>
除非我要链接到今天,如果是今天,它将仅指向 www.myurl.com/agenda,女巫是 CnfigurationManager.AppSettings 的值[“事件网址”]。我做错了什么?就像今天一样,它指向默认议程...
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您的路由和
Url.Action()
调用都完全按照您的预期工作:当路由数据与默认数据相同时,它会从 URL 中省略。由于您将DateTime.Now.Day
等指定为默认值,因此当链接到今天的议程时,它不会包含任何日期值。但是,这仍然会按照您想要的方式运行。如果您单击今天议程的链接,您实际上会看到今天的议程 - 只是不在 URL 中显示。
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 giveDateTime.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.
需要研究的一件事是您是否真的需要在路由中包含所有这些默认值,因为它们是问题的根源。例如,如果您只是从第二条路线中删除“day = DateTime.Now.Day”默认值,会发生什么?
您需要决定您希望 URL 做什么,例如,
您希望它显示 4/2010 的议程,还是希望它显示今天的 AgendaDetail?
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.
Do you want that to show the agenda for 4/2010, or do you want it to show today's AgendaDetail?