Class LinkHelper
- Namespace
- Dynamicweb.Environment.Helpers
- Assembly
- Dynamicweb.Core.dll
Class LinkHelper is a utility for parsing and manipulating links.
public class LinkHelper
- Inheritance
-
LinkHelper
- Inherited Members
Methods
AddToQueryString(string)
Adds a parameter to the query string in the current request context and returns the combined result.
public static string AddToQueryString(string addThis)
Parameters
addThisstringThe parameter and value to add to the query string. Example: ProductId=123.
Returns
- string
The combined query string. Example: ?ID=12&ProductId=123
AddToQueryString(string, string)
Adds a parameter to the passed query string and returns the combined result.
public static string AddToQueryString(string queryString, string addThis)
Parameters
queryStringstringThe existing query string where the new part should be added. Ie. ?ID=12
addThisstringThe parameter and vaulue to add to the querystring, i.e. Productid=123.
Returns
- string
The combined querystring i.e ?ID=12&Productid=123
Remarks
http://cc.davelozinski.com/c-sharp/c-fastest-way-to-check-last-character-of-a-string https://theburningmonk.com/2012/05/performance-test-string-contains-vs-string-indexof-vs-regex-ismatch/
AppendRemainingQueryWithoutId(string, IEnumerable<KeyValuePair<string, IEnumerable<string>>>)
Appends all remaining query parameters (excluding the parameter named 'id') to a base URL.
public static string AppendRemainingQueryWithoutId(string baseUrlString, IEnumerable<KeyValuePair<string, IEnumerable<string>>> queryParams)
Parameters
baseUrlStringstringThe base URL (without query string) to append to.
queryParamsIEnumerable<KeyValuePair<string, IEnumerable<string>>>A collection of query parameter key/value pairs where each key can have multiple values. Values that are null or empty strings are skipped.
Returns
- string
The base URL unchanged if there are no remaining parameters, otherwise the base URL followed by '?' and the encoded query string.
GetHttpDomain()
Returns the base domain url, including the scheme, host and port (if port is non-default) of the current request url
public static string GetHttpDomain()
Returns
- string
A domain url, e.g. https://www.dynamicweb.com
Exceptions
- ContextUnavailableException
Thrown if this method is used in a scenario without a HttpContext
GetInternalPageId(string)
Extracts the internal Dynamicweb page ID from a given URL.
This method supports multiple internal URL formats used by Dynamicweb, such as:
- "Default.aspx?ID=123"
- "/default.aspx?id=123"
- "default.asp?ID=123"
- Just the numeric page ID itself, e.g. "123"
The method is case-insensitive and ignores extra query parameters or trailing fragments. Returns 0 if the URL is invalid or does not contain an internal ID.
public static int GetInternalPageId(string url)
Parameters
urlstringThe URL to analyze.
Returns
- int
The internal page ID as an integer, or 0 if not found.
GetInternalPageIdsFromText(string)
Gets a collection of internal page IDs from the text.
public static List<int> GetInternalPageIdsFromText(string text)
Parameters
textstringThe text with one or some internal links
Returns
GetInternalPageUrl(int?)
Gets a domain-relative url for the page with the given pageId.
Format: Default.aspx?ID=pageId
public static string GetInternalPageUrl(int? pageId)
Parameters
pageIdint?The ID of the page to get the url for.
Returns
- string
A string representing a url for a page with the given ID.
Remarks
The pageId is not validated for whether the page exists.
GetInternalParagraphId(string?)
Returns the internal ID of the paragraph in the specified URL.
public static int GetInternalParagraphId(string? url)
Parameters
urlstringThe URL to parse, i.e. "Default.aspx?ID=123#456".
Returns
- int
The paragraph ID (456) from the
url.
GetInternalParagraphUrl(int, int)
Gets a domain-relative url for the paragraph with the given pageId and paragraphId.
Format: Default.aspx?ID=pageId#paragraphId
public static string GetInternalParagraphUrl(int pageId, int paragraphId)
Parameters
pageIdintThe ID of the page to build the url.
paragraphIdintThe ID of the paragraph to build the url.
Returns
- string
A string representing a url for a paragraph with the given IDs.
Remarks
The pageId and paragraphId are not validated for whether the page exists.
GetUrlWithPort(string, bool)
Gets an absolute url with port number, if port number is different from the default port for the scheme.
public static string GetUrlWithPort(string uri, bool disablePortNumber)
Parameters
Returns
- string
Absolute url with an optional port number
Remarks
A relative url will be converted into an absolute url using the base domain url of the current request
Exceptions
- ArgumentException
Thrown if
uricannot be parsed as a valid relative or absolute URI- ArgumentNullException
Thrown if
uriis null- ContextUnavailableException
Thrown if this method is used in a scenario without a HttpContext
IsLinkInternal(string)
Determines whether a given URL points to an internal page in the current Dynamicweb site.
Internal links include:
- Query links starting with '?', e.g. "?ID=123" "/Default.aspx?ID=123", "Default.aspx?AreaID=2"
- Numeric URLs that represent page IDs directly, e.g. "123"
The check is case-insensitive and ignores fragment identifiers (#anchors).
public static bool IsLinkInternal(string url)
Parameters
urlstringThe URL to examine.
Returns
- bool
True if the URL refers to a local page within the same Dynamicweb site; otherwise, false.
IsLinkInternalFile(string)
Determines whether the supplied link points to an internal file resource.
public static bool IsLinkInternalFile(string link)
Parameters
linkstringThe link string to evaluate (e.g. "/files/images/logo.png").
Returns
- bool
trueif the link starts with "/files/" (case-insensitive); otherwisefalse, including whenlinkis null, empty, or whitespace.
Remarks
A link is considered an internal file link if it begins with the folder segment "/files/" (case-insensitive). The method does not check for file existence or validate the remaining path segments.
IsLinkInternalParagraph(string?)
Checks if an URL is an internal paragraph of the current website.
public static bool IsLinkInternalParagraph(string? url)
Parameters
urlstringThe URL to be checked.
Returns
- bool
Returns
trueif the URL is a paragraph of the current website, i.e. Default.aspx?ID=123#456
IsLinkInternalProductOrGroup(string)
Determines whether the supplied link string represents an internal Dynamicweb product or group link.
public static bool IsLinkInternalProductOrGroup(string link)
Parameters
linkstringThe link string to evaluate (e.g. "ProductID=123" or "GroupID=45").
Returns
- bool
trueif the link starts with "ProductID=" or "GroupID=" (case-insensitive); otherwisefalse, including whenlinkis null, empty, or whitespace.
Remarks
A link is considered internal if it starts with either "ProductID=" or "GroupID=" (case-insensitive). The method does not validate the numeric value that may follow these prefixes; it only checks the prefix.
ParseQueryString(string)
public static NameValueCollection ParseQueryString(string query)
Parameters
querystring
Returns
ReplaceQueryString(string, string)
Replaces a value for a given key in the query string in the current request context.
public static string ReplaceQueryString(string element, string newValue)
Parameters
elementstringThe element key to replace value on. Example: productid.
newValuestringThe new value for the query string element key. Example: 567.
Returns
- string
The new query string with the value replaced. Example: ?ID=12&ProductId=567
ReplaceQueryString(string, string, string)
Replaces a value for a given key in the passed query string.
public static string ReplaceQueryString(string queryString, string element, string newValue)
Parameters
queryStringstringThe query string, i.e. ?id=123&productid=234.
elementstringThe element key to replace value on, i.e. productid.
newValuestringThe new value for the querystring element key, i.e. 567.
Returns
- string
The new querystring with the value replaced i.e ?ID=12&Productid=567
ReplaceQueryString(string, string, string, string)
Replaces a value for a given key in the passed query string.
public static string ReplaceQueryString(string urlAbsolutePath, string queryString, string element, string newValue)
Parameters
urlAbsolutePathstringAbsolute path from url.
queryStringstringThe query string, i.e. ?id=123&productid=234.
elementstringThe element key to replace value on, i.e. productid.
newValuestringThe new value for the querystring element key, i.e. 567.
Returns
- string
The new querystring with the value replaced i.e ?ID=12&Productid=567
StripQueryString(string)
Strips keys and their values from the query string in the current request context.
public static string StripQueryString(string itemsToRemove)
Parameters
itemsToRemovestringThe items to remove. i.e. ""productid,variantid"
Returns
- string
The new querystring with the keys and their values removed i.e ?ID=12
StripQueryString(string, string)
Strips keys and their values from the query string.
public static string StripQueryString(string queryString, string itemsToRemove)
Parameters
queryStringstringThe query string remove keys from, ie ?ID=123&productid=234&variantid=345.
itemsToRemovestringThe items to remove. i.e. ""productid,variantid"
Returns
- string
The new querystring with the keys and their values removed i.e ?ID=12
StripQueryString(string, string, string)
Strips keys and their values from the query string.
public static string StripQueryString(string urlAbsolutePath, string queryString, string itemsToRemove)
Parameters
urlAbsolutePathstringAbsolute path from url.
queryStringstringThe query string remove keys from, ie ?ID=123&productid=234&variantid=345.
itemsToRemovestringThe items to remove. i.e. ""productid,variantid"
Returns
- string
The new querystring with the keys and their values removed i.e ?ID=12