Class UrlPath
Provides utility methods to work with URL paths.
Inherited Members
Namespace: EmbedIO.Utilities
Syntax
public static class UrlPath
Fields
Root
The root URL path value, i.e. "/".
Declaration
public const string Root = "/"
Field Value
| Type | Description |
|---|---|
| String |
Methods
HasPrefix(String, String)
Determines whether the specified URL path is prefixed by the specified base URL path.
Declaration
public static bool HasPrefix(string path, string basePath)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
| String | basePath | The base URL path. |
Returns
| Type | Description |
|---|---|
| Boolean | true if |
Remarks
This method returns true even if the two URL paths are equivalent,
for example if both are "/", or if path is "/download" and
basePath is "/download/".
If you are sure that both path and basePath
are valid and normalized, for example because you have called UrlPath(String, String, Boolean),
then you may call UnsafeHasPrefix(String, String) instead of this method. UnsafeHasPrefix(String, String)
is slightly faster because it skips validity checks.
See Also
IsValid(String)
Determines whether a string is a valid URL path.
Declaration
public static bool IsValid(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
Returns
| Type | Description |
|---|---|
| Boolean | true if the specified URL path is valid; otherwise, false. |
Remarks
For a string to be a valid URL path, it must not be null,
must not be empty, and must start with a slash (/) character.
To ensure that a method parameter is a valid URL path, use UrlPath(String, String, Boolean).
See Also
Normalize(String, Boolean)
Normalizes the specified URL path.
Declaration
public static string Normalize(string path, bool isBasePath)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
| Boolean | isBasePath | if set to true, treat the URL path
as a base path, i.e. ensure it ends with a slash ( |
Returns
| Type | Description |
|---|---|
| String | The normalized path. |
Remarks
A normalized URL path is one where each run of two or more slash
(/) characters has been replaced with a single slash character.
This method does NOT try to decode URL-encoded characters.
If you are sure that path is a valid URL path,
for example because you have called IsValid(String) and it returned
true, then you may call UnsafeNormalize(String, Boolean)
instead of this method. UnsafeNormalize(String, Boolean) is slightly faster because
it skips the initial validity check.
There is no need to call this method for a method parameter for which you have already called UrlPath(String, String, Boolean).
See Also
Split(String)
Splits the specified URL path into segments.
Declaration
public static IEnumerable<string> Split(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
Returns
| Type | Description |
|---|---|
| IEnumerable<String> | An enumeration of path segments. |
Remarks
A root URL path (/) will result in an empty enumeration.
The returned enumeration will be the same whether path is a base URL path or not.
If you are sure that path is valid and normalized,
for example because you have called UrlPath(String, String, Boolean),
then you may call UnsafeSplit(String) instead of this method. UnsafeSplit(String)
is slightly faster because it skips validity checks.
See Also
StripPrefix(String, String)
Strips a base URL path fom a URL path, obtaining a relative path.
Declaration
public static string StripPrefix(string path, string basePath)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
| String | basePath | The base URL path. |
Returns
| Type | Description |
|---|---|
| String | The relative path, or null if |
Remarks
The returned relative path is NOT prefixed by a slash (/) character.
If path and basePath are equivalent,
for example if both are "/", or if path is "/download"
and basePath is "/download/", this method returns an empty string.
If you are sure that both path and basePath
are valid and normalized, for example because you have called UrlPath(String, String, Boolean),
then you may call UnsafeStripPrefix(String, String) instead of this method. UnsafeStripPrefix(String, String)
is slightly faster because it skips validity checks.
See Also
UnsafeHasPrefix(String, String)
Determines whether the specified URL path is prefixed by the specified base URL path, assuming both paths are valid and normalized.
Declaration
public static bool UnsafeHasPrefix(string path, string basePath)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
| String | basePath | The base URL path. |
Returns
| Type | Description |
|---|---|
| Boolean | true if |
Remarks
Unless both path and basePath are valid,
normalized URL paths, the behavior of this method is unspecified. You should call this method
only after calling either Normalize(String, Boolean) or UrlPath(String, String, Boolean)
to check and normalize both parameters.
If you are not sure about the validity and/or normalization of parameters, call HasPrefix(String, String) instead of this method.
This method returns true even if the two URL paths are equivalent,
for example if both are "/", or if path is "/download" and
basePath is "/download/".
See Also
UnsafeNormalize(String, Boolean)
Normalizes the specified URL path, assuming that it is valid.
Declaration
public static string UnsafeNormalize(string path, bool isBasePath)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
| Boolean | isBasePath | if set to true, treat the URL path
as a base path, i.e. ensure it ends with a slash ( |
Returns
| Type | Description |
|---|---|
| String | The normalized path. |
Remarks
A normalized URL path is one where each run of two or more slash
(/) characters has been replaced with a single slash character.
This method does NOT try to decode URL-encoded characters.
If path is not valid, the behavior of
this method is unspecified. You should call this method only after
IsValid(String) has returned true
for the same path.
You should call Normalize(String, Boolean) instead of this method
if you are not sure that path is valid.
There is no need to call this method for a method parameter for which you have already called UrlPath(String, String, Boolean).
See Also
UnsafeSplit(String)
Splits the specified URL path into segments, assuming it is valid and normalized.
Declaration
public static IEnumerable<string> UnsafeSplit(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
Returns
| Type | Description |
|---|---|
| IEnumerable<String> | An enumeration of path segments. |
Remarks
Unless path is a valid, normalized URL path,
the behavior of this method is unspecified. You should call this method
only after calling either Normalize(String, Boolean) or UrlPath(String, String, Boolean)
to check and normalize both parameters.
If you are not sure about the validity and/or normalization of path,
call StripPrefix(String, String) instead of this method.
A root URL path (/) will result in an empty enumeration.
The returned enumeration will be the same whether path is a base URL path or not.
See Also
UnsafeStripPrefix(String, String)
Strips a base URL path fom a URL path, obtaining a relative path, assuming both paths are valid and normalized.
Declaration
public static string UnsafeStripPrefix(string path, string basePath)
Parameters
| Type | Name | Description |
|---|---|---|
| String | path | The URL path. |
| String | basePath | The base URL path. |
Returns
| Type | Description |
|---|---|
| String | The relative path, or null if |
Remarks
Unless both path and basePath are valid,
normalized URL paths, the behavior of this method is unspecified. You should call this method
only after calling either Normalize(String, Boolean) or UrlPath(String, String, Boolean)
to check and normalize both parameters.
If you are not sure about the validity and/or normalization of parameters, call StripPrefix(String, String) instead of this method.
The returned relative path is NOT prefixed by a slash (/) character.
If path and basePath are equivalent,
for example if both are "/", or if path is "/download"
and basePath is "/download/", this method returns an empty string.