Table of Contents

Class UH

Namespace
SunamoUri
Assembly
SunamoUri.dll

URI Helper class providing methods for URL manipulation, encoding, and parsing.

public class UH
Inheritance
UH
Inherited Members
Extension Methods

Properties

UrlDecoded

Gets or sets the last URL-decoded result from IsUrlEncoded.

public static string? UrlDecoded { get; set; }

Property Value

string

Methods

AppendHttpIfNotExists(string)

Prepends http:// to the text if it does not already start with http.

public static string AppendHttpIfNotExists(string text)

Parameters

text string

The text to process.

Returns

string

The text with http:// prefix.

AppendHttpsIfNotExists(string)

Prepends https:// to the text if it does not already start with https.

public static string AppendHttpsIfNotExists(string text)

Parameters

text string

The text to process.

Returns

string

The text with https:// prefix.

BeforeCombine(ref string)

Ensures the host URL starts with https:// and ends with /.

public static void BeforeCombine(ref string hostApp)

Parameters

hostApp string

The host application URL.

ChangeExtension(string, string, string)

Changes the extension of a URI path.

public static string ChangeExtension(string text, string oldExtension, string newExtension)

Parameters

text string

The URI path to modify.

oldExtension string

The current extension to remove.

newExtension string

The new extension to append.

Returns

string

The URI path with the changed extension.

Combine(bool, params string[])

Combines URI segments with a slash separator, handling trailing slashes.

public static string Combine(bool isDirectory, params string[] segments)

Parameters

isDirectory bool

Whether to append a trailing slash for directory paths.

segments string[]

The URI segments to combine.

Returns

string

The combined URI.

Combine(IList<string>)

Combines a list of URI segments, adding trailing slashes to non-file segments.

public static string Combine(IList<string> list)

Parameters

list IList<string>

The list of URI segments to combine.

Returns

string

The combined URI.

Combine(params string[])

Combines URI segments with automatic trailing slash handling.

public static string Combine(params string[] segments)

Parameters

segments string[]

The URI segments to combine.

Returns

string

The combined URI.

CombineTrimEndSlash(params string[])

Combines URI segments, trimming trailing slashes from each segment.

public static string CombineTrimEndSlash(params string[] segments)

Parameters

segments string[]

The URI segments to combine.

Returns

string

The combined URI without a trailing slash.

Contains(ILogger, Uri, string, string, params string[])

Checks if a URI matches specified hostname, path, and query string conditions.

public static bool Contains(ILogger logger, Uri source, string hostnameEndsWith, string pathContains, params string[] queryStringContainsAll)

Parameters

logger ILogger

The logger for error reporting.

source Uri

The source URI to check.

hostnameEndsWith string

The expected hostname suffix.

pathContains string

The expected path substring.

queryStringContainsAll string[]

All query string values that must be present.

Returns

bool

True if all conditions match.

CreateUri(ILogger, string)

Creates a Uri instance from a text string, logging an error if creation fails.

public static Uri? CreateUri(ILogger logger, string text)

Parameters

logger ILogger

The logger for error reporting.

text string

The text to parse as a URI.

Returns

Uri

The created Uri, or null if parsing fails.

DebugLocalhost(string)

Converts the text to lowercase with the first character uppercased, then resolves it against UriShortConsts to produce a debug localhost URL.

public static string DebugLocalhost(string text)

Parameters

text string

The text to process.

Returns

string

The resolved debug URL.

GetDirectoryName(string)

Gets the directory path from a URI (the path up to the last slash). Returns the path with a trailing slash by convention.

public static string GetDirectoryName(string text)

Parameters

text string

The URI to extract the directory from.

Returns

string

The directory path with trailing slash.

GetExtension(string)

Gets the file extension from a URI.

public static string GetExtension(string text)

Parameters

text string

The URI to extract the extension from.

Returns

string

The file extension including the leading dot.

GetFileName(string, bool)

Gets the file name from a URI, optionally returning the full URL without query string.

public static string GetFileName(string text, bool isReturningWholeUrl = false)

Parameters

text string

The URI to extract the file name from.

isReturningWholeUrl bool

If true, returns the full URL without query string instead of just the file name.

Returns

string

The file name or full URL without query string.

GetFileNameWithoutExtension(string)

Gets the file name without extension from a URI.

public static string GetFileNameWithoutExtension(string text)

Parameters

text string

The URI to extract the file name from.

Returns

string

The file name without its extension.

GetFilePathAsHttpRequest(Uri)

Gets the local file path portion of the URI. Returns the same result as GetPageNameFromUri.

public static string GetFilePathAsHttpRequest(Uri uri)

Parameters

uri Uri

The URI to extract the path from.

Returns

string

The local path of the URI.

GetHost(ILogger, string)

Gets the host name from a URI text.

public static string GetHost(ILogger logger, string text)

Parameters

logger ILogger

The logger for error reporting.

text string

The URI text.

Returns

string

The host name.

GetPageNameFromUri(Uri)

Gets the page path from a URI without the query string.

public static string GetPageNameFromUri(Uri uri)

Parameters

uri Uri

The URI to extract the page name from.

Returns

string

The path and query without query string parameters.

GetPathname(string)

Gets the pathname from a URI by removing the protocol and host.

public static string GetPathname(string uri)

Parameters

uri string

The URI to extract the path from.

Returns

string

The pathname portion of the URI.

GetProtocolString(Uri)

Gets the protocol string (scheme with "://") from a URI.

public static string GetProtocolString(Uri uri)

Parameters

uri Uri

The URI to extract the protocol from.

Returns

string

The protocol string (e.g., "https://").

GetQueryAsHttpRequest(Uri)

Returns the complete query string including the leading question mark.

public static string GetQueryAsHttpRequest(Uri uri)

Parameters

uri Uri

The URI to extract the query from.

Returns

string

The query string including the leading question mark.

GetToken(string, int)

Gets a path segment from a URI at the specified offset from the end.

public static string GetToken(string text, int offset)

Parameters

text string

The URI to process.

offset int

The negative offset from the end of the segments.

Returns

string

The path segment at the specified offset.

GetUriSafeString(string)

Converts a title string to a URI-safe slug with a maximum length of 80 characters.

public static string GetUriSafeString(string title)

Parameters

title string

The title to convert.

Returns

string

A URI-safe slug string.

GetUriSafeString(string, int)

Converts a title string to a URI-safe slug with a specified maximum length.

public static string GetUriSafeString(string title, int maxLength)

Parameters

title string

The title to convert.

maxLength int

The maximum length of the resulting slug.

Returns

string

A URI-safe slug string.

GetUriSafeString(string, int, Func<string, bool>)

Converts a tag name to a URI-safe slug, ensuring uniqueness by appending a number if needed.

public static string GetUriSafeString(string tagName, int maxLength, Func<string, bool> methodInWebExists)

Parameters

tagName string

The tag name to convert.

maxLength int

The maximum length of the resulting slug.

methodInWebExists Func<string, bool>

A function that checks whether the slug already exists.

Returns

string

A unique URI-safe slug string.

HasHttpProtocol(string)

Returns true if the text has an http or https protocol prefix.

public static bool HasHttpProtocol(string text)

Parameters

text string

The text to check.

Returns

bool

True if the text starts with http:// or https://.

HostUriToPascalConvention(ILogger, string)

Converts a host URI to PascalCase convention.

public static string HostUriToPascalConvention(ILogger logger, string text)

Parameters

logger ILogger

The logger for error reporting.

text string

The URI text to convert.

Returns

string

The host name in PascalCase.

InsertBetweenPathAndFile(string, string)

Inserts a text segment between the path and file name in a URI.

public static string InsertBetweenPathAndFile(string uri, string textToInsert)

Parameters

uri string

The URI to modify.

textToInsert string

The text segment to insert.

Returns

string

The modified URI with the inserted segment.

IsUri(ILogger, string)

Checks whether the text can be parsed as a valid URI.

public static bool IsUri(ILogger logger, string text)

Parameters

logger ILogger

The logger for error reporting.

text string

The text to validate as a URI.

Returns

bool

True if the text is a valid URI.

IsUrlEncoded(string)

Checks whether a URI is URL-encoded. The decoded result is stored in UrlDecoded.

public static bool IsUrlEncoded(string uri)

Parameters

uri string

The URI to check.

Returns

bool

True if the URI is URL-encoded.

IsValidUriAndDomainIs(string, string, out bool)

Validates whether the text is a valid URI and its domain matches the expected domain.

public static bool IsValidUriAndDomainIs(string text, string domain, out bool isSurelyDomain)

Parameters

text string

The text to validate as a URI.

domain string

The expected domain, or "*" to accept any domain.

isSurelyDomain bool

Set to true if the URI was confirmed as a valid domain.

Returns

bool

True if the text is a valid URI with the expected domain.

IsWellFormedUriString(ref string, UriKind)

Checks if the URI is well-formed and trims it.

public static bool IsWellFormedUriString(ref string uri, UriKind uriKind)

Parameters

uri string

The URI to validate (modified in place).

uriKind UriKind

The kind of URI to validate against.

Returns

bool

True if the URI is well-formed.

KeepOnlyHostAndProtocol(string)

Keeps only the host and protocol from a URI, removing the path, trailing slash, and www prefix.

public static string KeepOnlyHostAndProtocol(string text)

Parameters

text string

The URI to process.

Returns

string

The host with protocol only.

RemoveHostAndProtocol(Uri)

Removes the host and protocol from a URI, returning only the path.

public static string RemoveHostAndProtocol(Uri uri)

Parameters

uri Uri

The URI to process.

Returns

string

The path portion of the URI.

RemoveLastChar(string)

Removes the last character from the text.

public static string RemoveLastChar(string text)

Parameters

text string

The text to process.

Returns

string

The text without its last character.

RemovePrefixHttpOrHttps(string)

Removes http:// or https:// prefix from the text.

public static string RemovePrefixHttpOrHttps(string text)

Parameters

text string

The text to process.

Returns

string

The text without the HTTP/HTTPS prefix.

RemovePrefixHttpOrHttps(string, out string)

Removes http:// or https:// prefix from the text, outputting the removed protocol. If no protocol is present, protocol is set to empty string.

public static string RemovePrefixHttpOrHttps(string text, out string protocol)

Parameters

text string

The text to process.

protocol string

The removed protocol string including "://", or empty if none found.

Returns

string

The text without the protocol prefix.

RemoveTrackingPart(string)

Removes the tracking part (utm parameters) from a URL and returns only the domain with protocol.

public static string RemoveTrackingPart(string text)

Parameters

text string

The URL to clean.

Returns

string

The URL without tracking parameters.

SanitizeKeepOnlyHost(string)

Sanitizes a URI to keep only the host name without protocol, www prefix, or path.

public static string SanitizeKeepOnlyHost(string text)

Parameters

text string

The URI to sanitize.

Returns

string

The host name only.

UrlDecode(string)

URL-decodes the text after trimming whitespace.

public static string UrlDecode(string text)

Parameters

text string

The text to decode.

Returns

string

The URL-decoded text.

UrlDecodeWithRemovePathSeparatorCharacter(string)

URL-decodes the text and removes path separator characters.

public static string UrlDecodeWithRemovePathSeparatorCharacter(string text)

Parameters

text string

The URL-encoded text to decode and clean.

Returns

string

The decoded and cleaned text.

UrlEncode(string)

URL-encodes the text after trimming whitespace.

public static string UrlEncode(string text)

Parameters

text string

The text to encode.

Returns

string

The URL-encoded text.

WhiteSpaceFromStart(string)

Extracts leading whitespace characters from the text.

public static string WhiteSpaceFromStart(string text)

Parameters

text string

The text to process.

Returns

string

A string containing only the leading whitespace.