Skip to content

Commit

Permalink
Rename "page" Razor and route variable to "pageNum" to work around as…
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel15 committed Oct 16, 2017
1 parent aaa41fe commit f5aecb0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
50 changes: 25 additions & 25 deletions Daniel15.Web/Controllers/BlogController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Daniel15.BusinessLayer.Services;
Expand Down Expand Up @@ -52,11 +52,11 @@ public BlogController(IBlogRepository blogRepository, IDisqusCommentRepository c
/// </summary>
/// <param name="posts">Posts to be displayed</param>
/// <param name="count">Number of posts being displayed</param>
/// <param name="page">Page number of the current page</param>
/// <param name="pageNum">Page number of the current pageNum</param>
/// <param name="viewName">Name of the view to render</param>
/// <param name="viewModel">View model to pass to the view</param>
/// <returns>Post listing</returns>
private ActionResult Listing(IEnumerable<PostModel> posts, int count, int page, string viewName = null, ListingViewModel viewModel = null)
private ActionResult Listing(IEnumerable<PostModel> posts, int count, int pageNum, string viewName = null, ListingViewModel viewModel = null)
{
if (viewName == null)
viewName = "Index";
Expand All @@ -66,7 +66,7 @@ private ActionResult Listing(IEnumerable<PostModel> posts, int count, int page,

var pages = (int)Math.Ceiling((double)count / ITEMS_PER_PAGE);

if (page > pages)
if (pageNum > pages)
return NotFound();

viewModel.Posts = posts.Select(post => new PostViewModel
Expand All @@ -76,7 +76,7 @@ private ActionResult Listing(IEnumerable<PostModel> posts, int count, int page,
SocialNetworks = GetSocialNetworks(post)
});
viewModel.TotalCount = count;
viewModel.Page = page;
viewModel.Page = pageNum;
viewModel.TotalPages = pages;
return View(viewName, viewModel);
}
Expand All @@ -86,27 +86,27 @@ private ActionResult Listing(IEnumerable<PostModel> posts, int count, int page,
/// </summary>
[ResponseCache(Location = ResponseCacheLocation.Any, Duration = ONE_HOUR)]
[Route("blog", Order = 1, Name = "BlogHome")]
[Route("blog/page-{page:int}", Order = 2, Name= "BlogHomePage")]
public virtual ActionResult Index(int page = 1)
[Route("blog/page-{pageNum:int}", Order = 2, Name= "BlogHomePage")]
public virtual ActionResult Index(int pageNum = 1)
{
var count = _blogRepository.PublishedCount();
var posts = _blogRepository.LatestPosts(ITEMS_PER_PAGE, (page - 1) * ITEMS_PER_PAGE);
return Listing(posts, count, page, "Index");
var posts = _blogRepository.LatestPosts(ITEMS_PER_PAGE, (pageNum - 1) * ITEMS_PER_PAGE);
return Listing(posts, count, pageNum, "Index");
}

/// <summary>
/// Viewing a category listing
/// </summary>
/// <param name="slug">Category slug</param>
/// <param name="page">Page number to view</param>
/// <param name="pageNum">Page number to view</param>
/// <param name="parentSlug">Slug of the category's parent</param>
/// <returns>Posts in this category</returns>
/// <remarks>These must be ordered AFTER the RSS rules in FeedController!</remarks>
[Route("category/{slug}", Order = 5, Name = "BlogCategory")]
[Route("category/{slug}/page-{page:int}", Order = 6, Name = "BlogCategoryPage")]
[Route("category/{slug}/page-{pageNum:int}", Order = 6, Name = "BlogCategoryPage")]
[Route("category/{parentSlug}/{slug}", Order = 7, Name = "BlogSubCategory")]
[Route("category/{parentSlug}/{slug}/page-{page:int}", Order = 8, Name = "BlogSubCategoryPage")]
public virtual ActionResult Category(string slug, int page = 1, string parentSlug = null)
[Route("category/{parentSlug}/{slug}/page-{pageNum:int}", Order = 8, Name = "BlogSubCategoryPage")]
public virtual ActionResult Category(string slug, int pageNum = 1, string parentSlug = null)
{
CategoryModel category;
try
Expand All @@ -122,12 +122,12 @@ public virtual ActionResult Category(string slug, int page = 1, string parentSlu
// If the category has a parent category, ensure it's in the URL
if (category.Parent != null && string.IsNullOrEmpty(parentSlug))
{
return RedirectPermanent(Url.BlogCategory(category, page));
return RedirectPermanent(Url.BlogCategory(category, pageNum));
}

var count = _blogRepository.PublishedCount(category);
var posts = _blogRepository.LatestPosts(category, ITEMS_PER_PAGE, (page - 1) * ITEMS_PER_PAGE);
return Listing(posts, count, page, "Category", new CategoryListingViewModel
var posts = _blogRepository.LatestPosts(category, ITEMS_PER_PAGE, (pageNum - 1) * ITEMS_PER_PAGE);
return Listing(posts, count, pageNum, "Category", new CategoryListingViewModel
{
Category = category,
RssUrl = Url.Action("BlogCategory", "Feed", new
Expand All @@ -142,11 +142,11 @@ public virtual ActionResult Category(string slug, int page = 1, string parentSlu
/// Viewing a listing of all posts tagged by a particular tag
/// </summary>
/// <param name="slug">Tag slug</param>
/// <param name="page">Page number to view</param>
/// <param name="pageNum">Page number to view</param>
/// <returns>Posts tagged with this tag</returns>
[Route("tag/{slug}", Order = 1, Name = "BlogTag")]
[Route("tag/{slug}/page-{page:int}", Order = 2, Name = "BlogTagPage")]
public virtual ActionResult Tag(string slug, int page = 1)
[Route("tag/{slug}/page-{pageNum:int}", Order = 2, Name = "BlogTagPage")]
public virtual ActionResult Tag(string slug, int pageNum = 1)
{
TagModel tag;
try
Expand All @@ -160,23 +160,23 @@ public virtual ActionResult Tag(string slug, int page = 1)
}

var count = _blogRepository.PublishedCount(tag);
var posts = _blogRepository.LatestPosts(tag, ITEMS_PER_PAGE, (page - 1) * ITEMS_PER_PAGE);
return Listing(posts, count, page, "Tag", new TagListingViewModel { Tag = tag });
var posts = _blogRepository.LatestPosts(tag, ITEMS_PER_PAGE, (pageNum - 1) * ITEMS_PER_PAGE);
return Listing(posts, count, pageNum, "Tag", new TagListingViewModel { Tag = tag });
}

/// <summary>
/// Viewing the blog archive (articles posted in the specified year and month)
/// </summary>
/// <param name="year">Year to get posts for</param>
/// <param name="month">Month to get posts for</param>
/// <param name="page">Page number to view</param>
/// <param name="pageNum">Page number to view</param>
/// <returns>Posts from this month</returns>
[Route("{year:int:length(4)}/{month:int:length(2)}")]
public virtual ActionResult Archive(int year, int month, int page = 1)
public virtual ActionResult Archive(int year, int month, int pageNum = 1)
{
var count = _blogRepository.PublishedCountForMonth(year, month);
var posts = _blogRepository.LatestPostsForMonth(year, month, ITEMS_PER_PAGE, (page - 1) * ITEMS_PER_PAGE);
return Listing(posts, count, page, "Index");
var posts = _blogRepository.LatestPostsForMonth(year, month, ITEMS_PER_PAGE, (pageNum - 1) * ITEMS_PER_PAGE);
return Listing(posts, count, pageNum, "Index");
}

/// <summary>
Expand Down
42 changes: 21 additions & 21 deletions Daniel15.Web/Extensions/BlogUrlHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Daniel15.Data.Entities.Blog;
using Daniel15.Data.Entities.Blog;
using Microsoft.AspNetCore.Mvc;

namespace Daniel15.Web.Extensions
Expand Down Expand Up @@ -53,25 +53,25 @@ public static string BlogPostEdit(this IUrlHelper urlHelper, PostModel post)
/// Gets the URL to the index of the blog
/// </summary>
/// <param name="urlHelper">The URL helper</param>
/// <param name="page">Page number to link to</param>
/// <param name="pageNum">Page number to link to</param>
/// <returns>The URL</returns>
public static string BlogIndex(this IUrlHelper urlHelper, int page = 1)
public static string BlogIndex(this IUrlHelper urlHelper, int pageNum = 1)
{
return page == 1
return pageNum == 1
? urlHelper.RouteUrl("BlogHome")
: urlHelper.RouteUrl("BlogHomePage", new { page });
: urlHelper.RouteUrl("BlogHomePage", new { pageNum });
}

/// <summary>
/// Gets the URL to the specified blog category post listing
/// </summary>
/// <param name="urlHelper">The URL helper</param>
/// <param name="category">Category to link to</param>
/// <param name="page">Page number to link to</param>
/// <param name="pageNum">Page number to link to</param>
/// <returns>The URL</returns>
public static string BlogCategory(this IUrlHelper urlHelper, CategoryModel category, int page = 1)
public static string BlogCategory(this IUrlHelper urlHelper, CategoryModel category, int pageNum = 1)
{
return BlogCategory(urlHelper, "Category", category.Slug, category.Parent?.Slug, page);
return BlogCategory(urlHelper, "Category", category.Slug, category.Parent?.Slug, pageNum);
}

/// <summary>
Expand All @@ -90,25 +90,25 @@ public static string BlogCategoryFeed(this IUrlHelper urlHelper, CategoryModel c
/// </summary>
/// <param name="urlHelper">The URL helper</param>
/// <param name="parentSlug">Slug of the parent</param>
/// <param name="page">Page number to link to</param>
/// <param name="pageNum">Page number to link to</param>
/// <param name="routeType">Route type to link to (eg. "Category" or "CategoryFeed")</param>
/// <param name="slug">Slug of the category</param>
/// <returns>The URL</returns>
public static string BlogCategory(this IUrlHelper urlHelper, string routeType, string slug, string parentSlug, int page = 1)
public static string BlogCategory(this IUrlHelper urlHelper, string routeType, string slug, string parentSlug, int pageNum = 1)
{
// It's safer to explicitly use the correct route here, instead of relying on the ASP.NET
// routing engine to choose it.
if (string.IsNullOrEmpty(parentSlug))
{
return page == 1
return pageNum == 1
? urlHelper.RouteUrl("Blog" + routeType, new { slug })
: urlHelper.RouteUrl("Blog" + routeType + "Page", new { slug, page });
: urlHelper.RouteUrl("Blog" + routeType + "Page", new { slug, pageNum });
}
else
{
return page == 1
return pageNum == 1
? urlHelper.RouteUrl("BlogSub" + routeType, new { slug, parentSlug })
: urlHelper.RouteUrl("BlogSub" + routeType + "Page", new { slug, parentSlug, page });
: urlHelper.RouteUrl("BlogSub" + routeType + "Page", new { slug, parentSlug, pageNum });
}
}

Expand All @@ -117,25 +117,25 @@ public static string BlogCategory(this IUrlHelper urlHelper, string routeType, s
/// </summary>
/// <param name="urlHelper">The URL helper</param>
/// <param name="tag">Tag to link to</param>
/// <param name="page">Page number to link to</param>
/// <param name="pageNum">Page number to link to</param>
/// <returns>The URL</returns>
public static string BlogTag(this IUrlHelper urlHelper, TagModel tag, int page = 1)
public static string BlogTag(this IUrlHelper urlHelper, TagModel tag, int pageNum = 1)
{
return urlHelper.BlogTag(tag.Slug, page);
return urlHelper.BlogTag(tag.Slug, pageNum);
}

/// <summary>
/// Gets the URL to the specified blog tagged post listing
/// </summary>
/// <param name="urlHelper">The URL helper</param>
/// <param name="slug">Slug of the tag</param>
/// <param name="page">Page number to link to</param>
/// <param name="pageNum">Page number to link to</param>
/// <returns>The URL</returns>
public static string BlogTag(this IUrlHelper urlHelper, string slug, int page = 1)
public static string BlogTag(this IUrlHelper urlHelper, string slug, int pageNum = 1)
{
return page == 1
return pageNum == 1
? urlHelper.RouteUrl("BlogTag", new { slug })
: urlHelper.RouteUrl("BlogTagPage", new { slug, page });
: urlHelper.RouteUrl("BlogTagPage", new { slug, pageNum });
}
}
}
12 changes: 6 additions & 6 deletions Daniel15.Web/Views/Shared/_Pagination.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model Daniel15.Web.ViewModels.Shared.PaginationModel
@model Daniel15.Web.ViewModels.Shared.PaginationModel

@* Don't even show anything if there's only one page *@
@if (Model.TotalPages == 1)
Expand All @@ -17,15 +17,15 @@
<a href="@Model.UrlGenerator(Model.CurrentPage - 1)" rel="prev">Previous</a>
}

@for (var page = 1; page <= Model.TotalPages; page++)
@for (var pageNum = 1; pageNum <= Model.TotalPages; pageNum++)
{
if (page == Model.CurrentPage)
if (pageNum == Model.CurrentPage)
{
<strong>@page</strong>
<strong>@pageNum</strong>
}
else
{
<a href="@Model.UrlGenerator(page)">@page</a>
<a href="@Model.UrlGenerator(pageNum)">@pageNum</a>
}
}

Expand All @@ -38,4 +38,4 @@
{
<a href="@Model.UrlGenerator(Model.CurrentPage + 1)" rel="next">Next</a>
}
</p>
</p>

0 comments on commit f5aecb0

Please sign in to comment.