ASP.NET MVC 3 - 难以理解路由
我使用 NerdDinner 教程作为工作基础,在 MVC 3 中创建了一个系统。我不确定我是否完全理解路由。
一切都工作正常,直到我向我拥有的分页助手添加了一种排序。
这是 global.asax.cs,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"UpcomingKeyDates", // Route name
"KeyDates.mvc/{sortBy}/Page/{page}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
);
routes.MapRoute(
"Root", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
);
}
当您第一次导航到该页面时,我希望默认列表按事件日期升序排序(效果很好)。排序和分页也很好用。但是,当我使用此链接时...
<%: Html.ActionLink("Create New", "Create", "Home") %>
该链接仅指向同一页面。我需要添加新路线或修改现有路线吗?非常感谢任何帮助。
谢谢。
I've created a system in MVC 3 using the NerdDinner tutorial as a base to work off. I'm not sure I fully understand Routing.
Everything was working fine until I added a sort to the Pagination helper that I have.
Here is the global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"UpcomingKeyDates", // Route name
"KeyDates.mvc/{sortBy}/Page/{page}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
);
routes.MapRoute(
"Root", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
);
}
I want to default the list to sort by Event Date ascending when you first navigate to the page (which works fine). The sort and pagination also works fine. However, when I'm using this link...
<%: Html.ActionLink("Create New", "Create", "Home") %>
The link just directs to the same page. Do I need to add a new route, or amend an existing route? Any help much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认路由应始终出现在最后,并且是包罗万象的路由。它将自动捕获空路由,相当于
http://yourdomain.com/
默认路由应始终具有以下格式
另外,如果页面将是数字,您可以限制它使用正则表达式(见下文)。
简而言之,更改您的 Global.asax ,使其看起来像这样:
The default route should always appear last and is the catch-all route. It will automatically catch the empty route which is equivalent to
http://yourdomain.com/
The default route should always have the following format
Also, if the page is going to be a number, you can constrain it using a regular expression (see below).
In brief, change your
Global.asax
so it looks like this: