路由/T4MVC Url.Action() 问题

发布于 2024-08-29 01:36:13 字数 985 浏览 5 评论 0原文

我有这两条路线:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-09-05 01:36:13

实际上,您的路由和 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 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.

时光病人 2024-09-05 01:36:13

需要研究的一件事是您是否真的需要在路由中包含所有这些默认值,因为它们是问题的根源。例如,如果您只是从第二条路线中删除“day = DateTime.Now.Day”默认值,会发生什么?

您需要决定您希望 URL 做什么,例如,

www.myurl.com/agenda/2010/04

您希望它显示 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.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文