-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TDEAL-8: HttpRequestExtensions and other improvements #229
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1780b4e
Add methods to check the content from the request route values.
sarahelsaig c52a2ba
Add shortcut for registering MVC filters.
sarahelsaig b4ed516
Fix missing context in WidgetFilterBase.
sarahelsaig 6b838c7
Allow returning shapes.
sarahelsaig 83cd8db
Merge remote-tracking branch 'origin/dev' into issue/TDEAL-8
sarahelsaig 9c7c532
Document HttpContext extensions.
sarahelsaig 351efaa
Add GetContentItemAsnyc extension method.
sarahelsaig d40297d
Remove ambiguity.
sarahelsaig d582ffe
Fix typo and add jsonPath support.
sarahelsaig df6a5e7
Add TaxonomyHelper.GetAllChildren.
sarahelsaig 75eae53
bug fix
sarahelsaig 9d04ea4
Fix spacing.
sarahelsaig 1587e00
Merge remote-tracking branch 'origin/dev' into issue/TDEAL-8
sarahelsaig ae5bb56
Add GetQueryWithout extension.
sarahelsaig fd8c705
Additional query string extension methods.
sarahelsaig 31951db
Move AddAsyncResultFilter to AspNetCore.
sarahelsaig 3299584
unusing
sarahelsaig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
Lombiq.HelpfulLibraries.AspNetCore/Extensions/HttpRequestExtensions.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,65 @@ | ||
using Lombiq.HelpfulLibraries.Common.Utilities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
|
||
namespace Microsoft.AspNetCore.Http; | ||
|
||
public static class HttpRequestExtensions | ||
{ | ||
/// <summary> | ||
/// Returns the query string without the leading <c>?</c> and without the keys in <paramref name="keysToExclude"/>. | ||
/// </summary> | ||
public static string GetQueryWithout(this HttpRequest request, params string[] keysToExclude) | ||
{ | ||
var query = HttpUtility.ParseQueryString(request.QueryString.Value ?? string.Empty); | ||
return string.Join('&', query | ||
.AllKeys | ||
.Where(key => key != null && !keysToExclude.Exists(key.EqualsOrdinalIgnoreCase)) | ||
.Select(key => $"{HttpUtility.UrlEncode(key)}={HttpUtility.UrlEncode(query[key] ?? string.Empty)}")); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the current URL but appends a new query string entry. | ||
/// </summary> | ||
public static string GetLinkWithAdditionalQuery(this HttpRequest request, string queryString, string key, object value) | ||
{ | ||
queryString ??= string.Empty; | ||
if (queryString.StartsWith('?')) queryString = queryString[1..]; | ||
|
||
var pageQuery = string.IsNullOrEmpty(queryString) | ||
? StringHelper.CreateInvariant($"{key}={value}") | ||
: StringHelper.CreateInvariant($"&{key}={value}"); | ||
return $"{request.PathBase}{request.Path}?{queryString}{pageQuery}"; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the current URL but appends a new query string entry. | ||
/// </summary> | ||
public static string GetLinkWithAdditionalQuery(this HttpRequest request, string key, object value) => | ||
request.GetLinkWithAdditionalQuery(request.QueryString.Value, key, value); | ||
|
||
/// <summary> | ||
/// Returns the current URL excluding any existing query string entry with the key <paramref name="key"/>, and with | ||
/// a new <paramref name="key"/>-<paramref name="value"/> entry appended. | ||
/// </summary> | ||
public static string GetLinkWithDifferentQuery(this HttpRequest request, string key, object value) => | ||
request.GetLinkWithAdditionalQuery(request.GetQueryWithout(key), key, value); | ||
|
||
/// <summary> | ||
/// Returns the current URL but with the value of the query string entry of with the key <paramref name="key"/> | ||
/// cycled to the next item in the <paramref name="values"/>. This can be useful for example to generate table | ||
/// header links that cycle ascending-descending-unsorted sorting by that column. | ||
/// </summary> | ||
public static string GetLinkAndCycleQueryValue(this HttpRequest request, string key, params string[] values) | ||
{ | ||
var query = HttpUtility.ParseQueryString(request.QueryString.Value ?? string.Empty); | ||
|
||
var value = query[key] ?? string.Empty; | ||
var index = ((IList<string>)values).IndexOf(value); | ||
var newValue = values[(index + 1) % values.Length]; | ||
|
||
return request.GetLinkWithDifferentQuery(key, newValue); | ||
} | ||
} |
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be a better fit for the AspNetCore library?