ASP.NET MVC - 路由和 UrlHelper
我有以下路线
routes.MapRoute(
"GigDayListings", // Route name
"gig/list/{year}/{month}/{day}", // URL with parameters
new { controller = "Gig", action = "List" },
new
{
year = @"^[0-9]+$",
month = @"^[0-9]+$",
day = @"^[0-9]+$"
} // Parameter defaults
);
当我访问 URL 时
gig/list/2009/01/01
该路线完美匹配并且我的操作被调用。
在我看来,我有一个助手,它执行以下操作:
var urlHelper = new UrlHelper(ViewContext);
string url = urlHelper.RouteUrl(ViewContext.RouteData.Values);
生成的字符串是:
http://localhost:3539/gig/list?year=2005&month=01&day=01
为什么不是
http://localhost:3539/gig/list/2005/01/01
我做错了什么?
I have the following route
routes.MapRoute(
"GigDayListings", // Route name
"gig/list/{year}/{month}/{day}", // URL with parameters
new { controller = "Gig", action = "List" },
new
{
year = @"^[0-9]+$",
month = @"^[0-9]+$",
day = @"^[0-9]+$"
} // Parameter defaults
);
When I visit the URL
gig/list/2009/01/01
This route matches perfectly and my action is called.
Inside my view I have a helper which does the following:
var urlHelper = new UrlHelper(ViewContext);
string url = urlHelper.RouteUrl(ViewContext.RouteData.Values);
The string generated is:
http://localhost:3539/gig/list?year=2005&month=01&day=01
Why is it not
http://localhost:3539/gig/list/2005/01/01
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的问题是您没有在通话中指定路线名称。 尝试使用
带有路由名称的重载。
干杯!
I think your problem is that you didn't specify the route name in your call. Try to use
overload with route name.
Cheers!
您是否检查过,当您提供 gig/list/2008/01/01 时,它实际上正在使用 GigDayListings 路线? 也许它正在使用不同的
Have you checked that when you supply gig/list/2008/01/01 that it is actually using the GigDayListings route? Maybe it's using a different one