ASP.Net MVC 路由混乱
我的 ASP.Net MVC 2 站点有以下 URL:
http://localhost:1919/TimeEntry/SummaryForWeek/15/2011
http://localhost:1919/TimeEntry/SummaryForMonth/4/2011
这两个都点击此路由:
routes.MapRoute
(
"TimeEntryActionMonthYear", // Route name
"TimeEntry/{action}/{month}/{year}", // URL with parameters
new { controller = "TimeEntry", action = "Summary", month = UrlParameter.Optional, year = UrlParameter.Optional } // Parameter defaults
);
SummaryForWeek 的操作需要一个名为“weekNumber”的参数(上面 URL 中的 15 值),而正在使用的路由是传递名为“month”的参数,因此 weekSummary 的 URL 失败。为了使路由有效而将 SummaryForWeek 的参数逐周重命名为每月是没有意义的。
我是否应该向路由表添加一条路由,其中 {action} 被硬编码为“SummaryForWeeks”?这也没有意义,因为它会使路由表膨胀。
我不明白什么(我知道这让我可以发表一些创造性的评论)?我被困住了。
谢谢
汤姆
I have the following URLs for my ASP.Net MVC 2 site:
http://localhost:1919/TimeEntry/SummaryForWeek/15/2011
http://localhost:1919/TimeEntry/SummaryForMonth/4/2011
Both of these are hitting this route:
routes.MapRoute
(
"TimeEntryActionMonthYear", // Route name
"TimeEntry/{action}/{month}/{year}", // URL with parameters
new { controller = "TimeEntry", action = "Summary", month = UrlParameter.Optional, year = UrlParameter.Optional } // Parameter defaults
);
The action for SummaryForWeek is expecting a parameter named "weekNumber" (the 15 value in URL above) whereas the route that is being used is passing a parameter called "month" and consequently the URL for weekSummary fails. It doesn't make sense to rename the parameter for SummaryForWeek from week to month just so the route works.
Should I add a Route to route table where {action} is hardcoded for "SummaryForWeeks"? This doesn't make sense either as it bloats the route table.
What am I not understanding (I know this opens me up for some creative comments)? I'm stuck.
Thanks
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要么需要两条路线,要么需要为参数提供更通用的名称:
或类似的名称。那么这两个操作都必须采用这两个参数,一个参数解释为月份,另一个参数解释为星期。
You either need two routes, or you need a more generic name for your parameters:
or something similar. Then both actions would have to take these two parameters, one would interpret as month, the other as week.