Skip to content

Commit

Permalink
Added custom route name option
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnboland committed Feb 7, 2018
1 parent a6fd662 commit 8819a79
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/MvcPaging.Demo/Controllers/PagingController.cs
Expand Up @@ -95,5 +95,13 @@ public ActionResult Bootstrap3(int? page)
int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
return View(this.allProducts.ToPagedList(currentPageIndex, DefaultPageSize));
}

[Route("genericos", Order = 3, Name = "genericos")]
[Route("generic", Order = 4, Name = "generic")]
public ActionResult CustomRouteName(int? page)
{
int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
return View(this.allProducts.ToPagedList(currentPageIndex, DefaultPageSize));
}
}
}
2 changes: 2 additions & 0 deletions src/MvcPaging.Demo/Global.asax.cs
Expand Up @@ -10,6 +10,8 @@ public static void RegisterRoutes(RouteCollection routes)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();

routes.MapMvcAttributeRoutes();

routes.MapRoute("SimplePaging", "SimplePaging/{page}",
new { controller = "Paging", action = "Index", page = UrlParameter.Optional },
new[] { "MvcPaging.Demo.Controllers" });
Expand Down
8 changes: 5 additions & 3 deletions src/MvcPaging.Demo/MvcPaging.Demo.csproj
Expand Up @@ -19,7 +19,7 @@
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<TargetFrameworkProfile />
<UseIISExpress>false</UseIISExpress>
<UseIISExpress>true</UseIISExpress>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<IISExpressSSLPort />
Expand All @@ -28,6 +28,7 @@
<IISExpressUseClassicPipelineMode>false</IISExpressUseClassicPipelineMode>
<MvcProjectUpgradeChecked>true</MvcProjectUpgradeChecked>
<UseGlobalApplicationHostFile />
<Use64BitIISExpress />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -197,6 +198,7 @@
<Content Include="Views\Shared\ViewSource.cshtml" />
<Content Include="Scripts\jquery-3.1.1.slim.min.map" />
<Content Include="Scripts\jquery-3.1.1.min.map" />
<Content Include="Views\Paging\CustomRouteName.cshtml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MvcPaging\MvcPaging.csproj">
Expand Down Expand Up @@ -244,11 +246,11 @@
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>False</UseIIS>
<UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>49800</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost/MvcPaging</IISUrl>
<IISUrl>http://localhost:49801/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
Expand Down
35 changes: 35 additions & 0 deletions src/MvcPaging.Demo/Views/Paging/CustomRouteName.cshtml
@@ -0,0 +1,35 @@
@using MvcPaging.Demo.Models
@model IPagedList<MvcPaging.Demo.Models.Product>
@{
ViewBag.Title = "Browse all products";
}
<h2>@ViewBag.Title</h2>
<table class="grid">
<thead>
<tr>
<th>Product name</th>
<th>Category</th>
</tr>
</thead>
<tbody>
@foreach (var product in Model)
{
<tr>
<td>@product.Name</td>
<td>@product.Category</td>
</tr>}
</tbody>
</table>
<div class="pager">
@Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount).Options(o => o.CustomRouteName("generic"))
</div>

@{
var sourceModel = new ViewSourceViewModel();
sourceModel.RazorCode = @"
";
sourceModel.ControllerCode = @"
";
}

@Html.Partial("ViewSource", sourceModel)
3 changes: 2 additions & 1 deletion src/MvcPaging/Pager.cs
Expand Up @@ -234,7 +234,8 @@ protected virtual string GeneratePageUrl(int pageNumber)
pageLinkValueDictionary = pageLinkValueDictionary.FixListRouteDataValues();

// 'Render' virtual path.
var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext, pageLinkValueDictionary);
var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext,
this.pagerOptions.CustomRouteName, pageLinkValueDictionary);

return virtualPathForArea == null ? null : virtualPathForArea.VirtualPath;
}
Expand Down
6 changes: 6 additions & 0 deletions src/MvcPaging/PagerOptions.cs
Expand Up @@ -23,6 +23,7 @@ public static class DefaultDefaults
public const bool DisplayLastPage = false;
public const bool UseItemCountAsPageCount = false;
public static bool HidePreviousAndNextPage = false;
public const string CustomRouteName = null;
}

/// <summary>
Expand All @@ -46,6 +47,7 @@ public static class Defaults
public static bool DisplayFirstPage = DefaultDefaults.DisplayFirstPage;
public static bool DisplayLastPage = DefaultDefaults.DisplayLastPage;
public static bool UseItemCountAsPageCount = DefaultDefaults.UseItemCountAsPageCount;
public static string CustomRouteName = DefaultDefaults.CustomRouteName;

public static void Reset()
{
Expand All @@ -64,6 +66,7 @@ public static void Reset()
DisplayFirstPage = DefaultDefaults.DisplayFirstPage;
DisplayLastPage = DefaultDefaults.DisplayLastPage;
UseItemCountAsPageCount = DefaultDefaults.UseItemCountAsPageCount;
CustomRouteName = DefaultDefaults.CustomRouteName;
}
}

Expand Down Expand Up @@ -107,6 +110,8 @@ public static void Reset()

public bool UseItemCountAsPageCount { get; internal set; }

public string CustomRouteName { get; set; }

public PagerOptions()
{
RouteValues = new RouteValueDictionary();
Expand All @@ -126,6 +131,7 @@ public PagerOptions()
DisplayLastPage = DefaultDefaults.DisplayLastPage;
UseItemCountAsPageCount = DefaultDefaults.UseItemCountAsPageCount;
HidePreviousAndNextPage = DefaultDefaults.HidePreviousAndNextPage;
CustomRouteName = DefaultDefaults.CustomRouteName;
}
}
}
26 changes: 26 additions & 0 deletions src/MvcPaging/PagerOptionsBuilder.cs
Expand Up @@ -298,6 +298,21 @@ public PagerOptionsBuilder UseItemCountAsPageCount()
return this;
}

/// <summary>
/// Use an explicit route instead of letting ASP.NET MVC figuring out the correct route.
/// </summary>
/// <param name="customRouteName"></param>
/// <returns></returns>
public PagerOptionsBuilder CustomRouteName(string customRouteName)
{
if (customRouteName == null)
{
throw new ArgumentException("customRouteName may not be null", "customRouteName");
}
this.pagerOptions.CustomRouteName = customRouteName;
return this;
}

/// <summary>
/// Set the AjaxOptions.
/// </summary>
Expand Down Expand Up @@ -558,5 +573,16 @@ public new PagerOptionsBuilder<TModel> UseItemCountAsPageCount()
base.UseItemCountAsPageCount();
return this;
}

/// <summary>
/// Use an explicit route instead of letting ASP.NET MVC figuring out the correct route.
/// </summary>
/// <param name="customRouteName"></param>
/// <returns></returns>
public new PagerOptionsBuilder<TModel> CustomRouteName(string customRouteName)
{
base.CustomRouteName(customRouteName);
return this;
}
}
}

0 comments on commit 8819a79

Please sign in to comment.