ASP.Net MVC 中的分页和路由 [英] Paging and routing in ASP.Net MVC

查看:17
本文介绍了ASP.Net MVC 中的分页和路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Martijn Boland 的Paging withASP.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.

Martijn 说:

在内部,寻呼机使用RouteTable.Routes.GetVirtualPath() 到呈现 url 以便页面 url 可以通过路由配置来创建例如,漂亮的 url 就像'/Categories/Shoes/Page/1' 而不是'/Paging/ViewByCategory?name=Shoes&page=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,末尾带有一个页面"参数,而不是一个表示当前上下文的 url.

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.

此外,将这个‘/Paging/ViewByCategory?name=Shoes&page=1’更改为这个‘/Categories/Shoes/Page/1’的路由是什么样的?

Also what would the routing look like to change this ‘/Paging/ViewByCategory?name=Shoes&page=1′ to this ‘/Categories/Shoes/Page/1′ ?

推荐答案

我假设你有 Paging 控制器并且这个控制器有 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 将返回‘/Categories/Shoes/Page/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆