分页和路由在ASP.Net MVC [英] Paging and routing in ASP.Net MVC
问题描述
我下面的Martijn博兰的与ASP分页.NET MVC。虽然有用它已经提出了几个问题,我不明白。
I am following Martijn Boland's 'Paging with ASP.NET MVC'. And while helpful it has raised a couple of issues I don't understand.
马亭说:
在内部,寻呼机的用途
RouteTable.Routes.GetVirtualPath()来
使URL的这样的页面的URL即可
通过路由创建配置
好看的URL例如像
'/分类/鞋/页/ 1',而不是
。'/寻呼/ ViewByCategory名=鞋类及放大器;页= 1
Internally, the pager uses RouteTable.Routes.GetVirtualPath() to render the url’s so the page url’s can be configured via routing to create nice looking url’s like for example ‘/Categories/Shoes/Page/1′ instead of ‘/Paging/ViewByCategory?name=Shoes&page=1′.
这是是他在说什么:
private string GeneratePageLink(string linkText, int pageNumber)
{
var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary);
pageLinkValueDictionary.Add("page", pageNumber);
//var virtualPathData = this.viewContext.RouteData.Route.GetVirtualPath(this.viewContext, pageLinkValueDictionary);
var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary);
if (virtualPathData != null)
{
string linkFormat = "<a href=\"{0}\">{1}</a>";
return String.Format(linkFormat, virtualPathData.VirtualPath, linkText);
}
else
{
return null;
}
}
这是如何工作的?当我使用它只是virtualPathData.VirtualPath带回一个URL重新presenting在我的路由表中的第一个路由与末尾的页面参数而不是一个URL重新presenting当前上下文。
How does this work? When I use it virtualPathData.VirtualPath just brings back a url representing the first route in my routing table with a 'page' param on the end rather then a url representing the current context.
还有什么会路由看起来喜欢这个'/寻呼/ ViewByCategory名=鞋类及放?;页= 1'改变这种'/分类/鞋/页/ 1
Also what would the routing look like to change this ‘/Paging/ViewByCategory?name=Shoes&page=1′ to this ‘/Categories/Shoes/Page/1′ ?
推荐答案
我假设你已经呼叫控制器,这控制器具有ViewByCategory行动。
I assume You have Paging controller and this controller has ViewByCategory action.
ViewByCategory如下:
ViewByCategory looks like:
public ActionResult ViewByCategory(string categoryName, int? page)
{
....
}
路由看起来像
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"RouteByCategory",
"Categories/{categoryName}/Page/{page}",
new { controller = "Paging", action = "ViewByCategory" }
);
routes.MapRoute(
"RouteByCategoryFirstPage",
"Categories/{categoryName}",
new { controller = "Paging", action = "ViewByCategory", page = 1 }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
GeneratePageLink将在'/分类/鞋/页/ 1'格式返回链接,因为这是在路由表中第一个匹配的路由模式。
GeneratePageLink will return link in ‘/Categories/Shoes/Page/1′ format, because it is first matching route pattern in routing table.
这篇关于分页和路由在ASP.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!