【问题标题】:Replacing the query string in the URL generated by PagedList with custom URL将 PagedList 生成的 URL 中的查询字符串替换为自定义 URL
【发布时间】:2017-10-13 15:35:50
【问题描述】:

我在功能齐全的寻呼机中使用 PagedList,它按预期为索引页面生成查询字符串。

我想知道是否可以自定义 URL 以消除查询字符串并添加另一个 Route 参数。

以下是我的看法:-

@model PagedList.Core.IPagedList<ActionAugerMVC.Models.Review>
@using PagedList.Core.Mvc;
@addTagHelper *, PagedList.Core.Mvc

<div id='Paging' style="text-align:center">
     Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
     <pager class="pager-container" list="@Model" 
            options="@PagedListRenderOptions.TwitterBootstrapPager" 
            asp-action="Index" asp-controller="Review" />
</div> 

这是我的控制器动作:-

[Route("Reviews/calgary-tech-reviews")]
public IActionResult Index(int? page)
{
    int pageSize = 10;
    int pageIndex = 1;
    pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;

    var review = unitOfWork.ReviewRepository.GetAll();
    return View(review.OrderByDescending(m=> m.Date).ToPagedList(pageIndex,pageSize));
}

当前 URL 如下所示:-

http://localhost:63613/Reviews/calgary-tech-reviews?page=2    

我希望它看起来像这样:-

http://localhost:63613/Reviews/calgary-tech-reviews/page-2/

http://localhost:63613/Reviews/page-2/calgary-tech-reviews/

任何帮助将不胜感激,因为我还是新手! 再次感谢 !

【问题讨论】:

    标签: c# asp.net-mvc razor url-routing pagedlist


    【解决方案1】:

    Startup.cs 文件,方法Configure 上,添加获取该参数的路由:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        //...
        app.UseMvc(routes =>
        {
            //...
            routes.MapRoute(
                name: "paged",
                template: "{controller=Reviews}/{action=Index}/page/{page=1}");
        });
     }
    

    现在你可以打开:http://localhost:63613/Reviews/calgary-tech-reviews/page/2/

    我无法将其设置为与page-{page=1} 一起使用,只能与page/{page=1} 一起使用。 page 在第一种情况下总是返回 0

    如果你能做到,请告诉我。 =)

    【讨论】:

    • 让我试试,非常感谢您的帮助。
    • 它似乎没有任何影响,即使移除了 Route 装饰器。
    猜你喜欢
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    相关资源
    最近更新 更多