Quantcast
Channel: Customized Commerce 13 and earlier versions
Viewing all articles
Browse latest Browse all 9642

PartialRouter for Pagination

$
0
0

I have a page type of BlogListPage with a controller:

public class BlogListPageController : PageController<BlogListPage>
{
    public ActionResult Index(BlogListPage currentPage, int? page = 1, BlogFiltersRequest filters = null)
    {
        var model = new BlogListPageViewModel(currentPage);
        return View(PopulateViewModel(model, page ?? 1, filters));
    }
}

I am able to call this page: /blog and /blog?page=3, but I want to be able to call page 3 using this url: /blog/3

I have implemented a Partial Route and registed it

public class BlogPaginationPartialRouting : IPartialRouter<BlogListPage, BlogListPage>
{
    public object RoutePartial(BlogListPage content, UrlResolverContext urlResolverContext)
    {
        // get the next url segment, which should be the post url
        var continentPart = urlResolverContext.GetNextRemainingSegment(urlResolverContext.RemainingPath);
         // check if part is an int
        if (continentPart != null && int.TryParse(continentPart.Next, out int page))
        {
            // add page parameter
            urlResolverContext.RouteValues.Add("page", (int?)page);
            // set the rest of the path
            urlResolverContext.RemainingPath = continentPart.Remaining;
        }
        // return the blog or null
        return content;
    }
    public PartialRouteData GetPartialVirtualPath(BlogListPage content, UrlGeneratorContext urlGeneratorContext)
    {
        return new PartialRouteData
        {
            BasePathRoot = content.ContentLink,
            PartialVirtualPath = string.Empty
         };
    }
}
_services.AddSingleton<IPartialRouter, BlogPaginationPartialRouting>();

When I run and set breakpoints and call /blog/3, BlogPaginationPartialRouting.RoutePartial() is hit and the page value is set. And the the controller Indexaction is hit, but the page parameter is set to the default of 1and not to 3.

How can I set the action parameter for the page value in the IPartialRouter?

I found this https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2017/3/custom-routing-with-extra-parameters-in-action-method post, but it is for older version.


Viewing all articles
Browse latest Browse all 9642