This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move convention extensions from RazorPagesOptions to PageConventionCo…
…llectionsExtensions Fixes #6462
- Loading branch information
Showing
18 changed files
with
660 additions
and
429 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/IPageConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationModels | ||
{ | ||
/// <summary> | ||
/// Common interface for route and application model conventions that apply to Razor Pages. | ||
/// </summary> | ||
public interface IPageConvention | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/PageConventionCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.ObjectModel; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationModels | ||
{ | ||
public class PageConventionCollection : Collection<IPageConvention> | ||
{ | ||
/// <summary> | ||
/// Creates and adds an <see cref="IPageApplicationModelConvention"/> that invokes an action on the | ||
/// <see cref="PageApplicationModel"/> for the page with the speciifed name. | ||
/// </summary> | ||
/// <param name="pageName">The name of the page e.g. <c>/Users/List</c></param> | ||
/// <param name="action">The <see cref="Action"/>.</param> | ||
/// <returns>The added <see cref="IPageApplicationModelConvention"/>.</returns> | ||
public IPageApplicationModelConvention AddPageApplicationModelConvention( | ||
string pageName, | ||
Action<PageApplicationModel> action) | ||
{ | ||
EnsureValidPageName(pageName); | ||
|
||
if (action == null) | ||
{ | ||
throw new ArgumentNullException(nameof(action)); | ||
} | ||
|
||
return Add(new PageApplicationModelConvention(pageName, action)); | ||
} | ||
|
||
/// <summary> | ||
/// Creates and adds an <see cref="IPageApplicationModelConvention"/> that invokes an action on | ||
/// <see cref="PageApplicationModel"/> instances for all page under the specified folder. | ||
/// </summary> | ||
/// <param name="folderPath">The path of the folder relative to the Razor Pages root. e.g. <c>/Users/</c></param> | ||
/// <param name="action">The <see cref="Action"/>.</param> | ||
/// <returns>The added <see cref="IPageApplicationModelConvention"/>.</returns> | ||
public IPageApplicationModelConvention AddFolderApplicationModelConvention(string folderPath, Action<PageApplicationModel> action) | ||
{ | ||
EnsureValidFolderPath(folderPath); | ||
|
||
if (action == null) | ||
{ | ||
throw new ArgumentNullException(nameof(action)); | ||
} | ||
|
||
return Add(new FolderApplicationModelConvention(folderPath, action)); | ||
} | ||
|
||
/// <summary> | ||
/// Creates and adds an <see cref="IPageRouteModelConvention"/> that invokes an action on the | ||
/// <see cref="PageRouteModel"/> for the page with the speciifed name. | ||
/// </summary> | ||
/// <param name="pageName">The name of the page e.g. <c>/Users/List</c></param> | ||
/// <param name="action">The <see cref="Action"/>.</param> | ||
/// <returns>The added <see cref="IPageRouteModelConvention"/>.</returns> | ||
public IPageRouteModelConvention AddPageRouteModelConvention(string pageName, Action<PageRouteModel> action) | ||
{ | ||
EnsureValidPageName(pageName); | ||
|
||
if (action == null) | ||
{ | ||
throw new ArgumentNullException(nameof(action)); | ||
} | ||
|
||
return Add(new PageRouteModelConvention(pageName, action)); | ||
} | ||
|
||
/// <summary> | ||
/// Creates and adds an <see cref="IPageRouteModelConvention"/> that invokes an action on | ||
/// <see cref="PageRouteModel"/> instances for all page under the specified folder. | ||
/// </summary> | ||
/// <param name="folderPath">The path of the folder relative to the Razor Pages root. e.g. <c>/Users/</c></param> | ||
/// <param name="action">The <see cref="Action"/>.</param> | ||
/// <returns>The added <see cref="IPageApplicationModelConvention"/>.</returns> | ||
public IPageRouteModelConvention AddFolderRouteModelConvention(string folderPath, Action<PageRouteModel> action) | ||
{ | ||
EnsureValidFolderPath(folderPath); | ||
|
||
if (action == null) | ||
{ | ||
throw new ArgumentNullException(nameof(action)); | ||
} | ||
|
||
return Add(new FolderRouteModelConvention(folderPath, action)); | ||
} | ||
|
||
// Internal for unit testing | ||
internal static void EnsureValidPageName(string pageName) | ||
{ | ||
if (string.IsNullOrEmpty(pageName)) | ||
{ | ||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(pageName)); | ||
} | ||
|
||
if (pageName[0] != '/' || pageName.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
throw new ArgumentException(Resources.FormatInvalidValidPageName(pageName), nameof(pageName)); | ||
} | ||
} | ||
|
||
// Internal for unit testing | ||
internal static void EnsureValidFolderPath(string folderPath) | ||
{ | ||
if (string.IsNullOrEmpty(folderPath)) | ||
{ | ||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(folderPath)); | ||
} | ||
|
||
if (folderPath[0] != '/') | ||
{ | ||
throw new ArgumentException(Resources.PathMustBeRootRelativePath, nameof(folderPath)); | ||
} | ||
} | ||
|
||
private TConvention Add<TConvention>(TConvention convention) where TConvention: IPageConvention | ||
{ | ||
base.Add(convention); | ||
return convention; | ||
} | ||
|
||
private class PageRouteModelConvention : IPageRouteModelConvention | ||
{ | ||
private readonly string _path; | ||
private readonly Action<PageRouteModel> _action; | ||
|
||
public PageRouteModelConvention(string path, Action<PageRouteModel> action) | ||
{ | ||
_path = path; | ||
_action = action; | ||
} | ||
|
||
public void Apply(PageRouteModel model) | ||
{ | ||
if (string.Equals(model.ViewEnginePath, _path, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
_action(model); | ||
} | ||
} | ||
} | ||
|
||
private class FolderRouteModelConvention : IPageRouteModelConvention | ||
{ | ||
private readonly string _folderPath; | ||
private readonly Action<PageRouteModel> _action; | ||
|
||
public FolderRouteModelConvention(string folderPath, Action<PageRouteModel> action) | ||
{ | ||
_folderPath = folderPath.TrimEnd('/'); | ||
_action = action; | ||
} | ||
|
||
public void Apply(PageRouteModel model) | ||
{ | ||
var viewEnginePath = model.ViewEnginePath; | ||
|
||
if (PathBelongsToFolder(_folderPath, viewEnginePath)) | ||
{ | ||
_action(model); | ||
} | ||
} | ||
} | ||
|
||
private class PageApplicationModelConvention : IPageApplicationModelConvention | ||
{ | ||
private readonly string _path; | ||
private readonly Action<PageApplicationModel> _action; | ||
|
||
public PageApplicationModelConvention(string path, Action<PageApplicationModel> action) | ||
{ | ||
_path = path; | ||
_action = action; | ||
} | ||
|
||
public void Apply(PageApplicationModel model) | ||
{ | ||
if (string.Equals(model.ViewEnginePath, _path, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
_action(model); | ||
} | ||
} | ||
} | ||
|
||
private class FolderApplicationModelConvention : IPageApplicationModelConvention | ||
{ | ||
private readonly string _folderPath; | ||
private readonly Action<PageApplicationModel> _action; | ||
|
||
public FolderApplicationModelConvention(string folderPath, Action<PageApplicationModel> action) | ||
{ | ||
_folderPath = folderPath.TrimEnd('/'); | ||
_action = action; | ||
} | ||
|
||
public void Apply(PageApplicationModel model) | ||
{ | ||
var viewEnginePath = model.ViewEnginePath; | ||
|
||
if (PathBelongsToFolder(_folderPath, viewEnginePath)) | ||
{ | ||
_action(model); | ||
} | ||
} | ||
} | ||
|
||
// Internal for unit testing | ||
internal static bool PathBelongsToFolder(string folderPath, string viewEnginePath) | ||
{ | ||
if (folderPath == "/") | ||
{ | ||
// Root directory covers everything. | ||
return true; | ||
} | ||
|
||
return viewEnginePath.Length > folderPath.Length && | ||
viewEnginePath.StartsWith(folderPath, StringComparison.OrdinalIgnoreCase) && | ||
viewEnginePath[folderPath.Length] == '/'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.