November Happy Hour will be moved to Thursday December 5th.

Class UriSupport

Contains various support functionality for dealing with URIs.

Inheritance
System.Object
UriSupport
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: EPiServer
Assembly: EPiServer.dll
Version: 9.12.2
Syntax
public class UriSupport
Remarks

See the UriSupport class methods which contain further information.

Examples

See the examples below:

Resolves the path relative the UI directory

            UriSupport.ResolveUrlFromUtilBySettings("javascript/episerverscriptmanager.js");

link a page to another language than the current language

            PageData languageBranch = new PageData(CurrentPage);            
    EPiServer.UriSupport.AddLanguageSelection(pd.LinkURL, pd.LanguageID);

link a page to another language than the current language

            // link a page to another language than the current language
    String NavigateUrl = EPiServer.UriSupport.AddLanguageSelection(languageBranch.LinkURL, languageBranch.LanguageID);

Constructors

UriSupport()

Declaration
public UriSupport()

Fields

HttpLocalhostUri

The HTTP URI to the local machine.

Declaration
public static readonly Uri HttpLocalhostUri
Field Value
Type Description
System.Uri

Properties

InternalUIUrl

Gets the URL to the UI folder.

Declaration
public static Url InternalUIUrl { get; }
Property Value
Type Description
Url

The internal UI URL.

Remarks

Use this cached representation of the UIUrl since accessing the configuration settings is very slow, and here you get the benefit of the Url class as well.

InternalUtilUrl

Gets the URL to the Util folder.

Declaration
public static Url InternalUtilUrl { get; }
Property Value
Type Description
Url

The internal util URL.

Remarks

Use this cached representation of the UtilUrl since accessing the configuration settings is very slow, and here you get the benefit of the Url class as well.

SiteDefinitionResolver

SiteDefinition Resolver

Declaration
public static SiteDefinitionResolver SiteDefinitionResolver { get; set; }
Property Value
Type Description
SiteDefinitionResolver

SiteUrl

Gets the site URL.

Declaration
public static Uri SiteUrl { get; }
Property Value
Type Description
System.Uri

The site URL.

UIUrl

Gets the UI URL.

Declaration
public static Uri UIUrl { get; }
Property Value
Type Description
System.Uri

The UI URL.

UtilUrl

Gets the util URL.

Declaration
public static Uri UtilUrl { get; }
Property Value
Type Description
System.Uri

The util URL.

Methods

AbsolutePathForSite(String, Settings)

Gets an absolute virtual path for the site given by siteSettings from the passed in virtual path.

Declaration
[Obsolete("Use AbsolutePathForSite (string, SiteDefinition )")]
public static string AbsolutePathForSite(string virtualPath, Settings siteSettings)
Parameters
Type Name Description
System.String virtualPath

The virtual path.

Settings siteSettings

The site settings.

Returns
Type Description
System.String
Remarks

If siteSettings then Settings.Instance is used.

AbsolutePathForSite(String, SiteDefinition)

Gets an absolute virtual path for the site given by siteDef from the passed in virtual path.

Declaration
public static string AbsolutePathForSite(string virtualPath, SiteDefinition siteDef)
Parameters
Type Name Description
System.String virtualPath

The virtual path.

SiteDefinition siteDef

The SiteDefinition.

Returns
Type Description
System.String
Remarks

If siteDef then SiteDefinition.Current is used.

AbsoluteUrlBySettings(String)

Get the absolute URL for the site by settings, including scheme, host and port

Declaration
public static string AbsoluteUrlBySettings(string path)
Parameters
Type Name Description
System.String path

The path, will be resolved if required.

Returns
Type Description
System.String

A fully qualified URI starting with scheme, based on Web.Config settings

AbsoluteUrlFromUIBySettings(String)

Get the absolute URL from UI by settings, including scheme, host and port

Declaration
public static string AbsoluteUrlFromUIBySettings(string path)
Parameters
Type Name Description
System.String path

The path, will be resolved from UI if required.

Returns
Type Description
System.String

A fully qualified URI starting with scheme, based on Web.Config settings

AbsoluteUrlFromUtilBySettings(String)

Get the absolute URL from UI by settings, including scheme, host and port

Declaration
public static string AbsoluteUrlFromUtilBySettings(string path)
Parameters
Type Name Description
System.String path

The path, will be resolved from Util if required.

Returns
Type Description
System.String

A fully qualified URI starting with scheme, based on Web.Config settings

AddLanguageSelection(String, String)

Adds an epslanguage parameter to the query string of a URL.

Declaration
public static string AddLanguageSelection(string url, string languageCode)
Parameters
Type Name Description
System.String url

The URL to add the parameter to.

System.String languageCode

The langauage code.

Returns
Type Description
System.String

The resulting URL as a string.

AddQueryString(String, String, String)

Add query string parameter to given url

Declaration
public static string AddQueryString(string url, string name, string val)
Parameters
Type Name Description
System.String url

The original url (may include query string parameters)

System.String name

Name of the query string parameter (for example "status")

System.String val

The value of the query string parameter

Returns
Type Description
System.String

The new url with the query string added.

Remarks

This method will replace the value of an existing query string with the same name.

AddValuesToCollection(NameValueCollection, NameValueCollection)

Adds values to a NameValueCollection from another NameValueCollection.

Declaration
public static void AddValuesToCollection(NameValueCollection addTo, NameValueCollection addFrom)
Parameters
Type Name Description
System.Collections.Specialized.NameValueCollection addTo

The collection where the values will be added

System.Collections.Specialized.NameValueCollection addFrom

The collection that contains the values that will be added

AddValuesToCollection(NameValueCollection, NameValueCollection, List<String>, List<String>)

Adds values to a NameValueCollection from another NameValueCollection.

Declaration
public static void AddValuesToCollection(NameValueCollection addTo, NameValueCollection addFrom, List<string> includeKeys, List<string> excludeKeys)
Parameters
Type Name Description
System.Collections.Specialized.NameValueCollection addTo

The collection where the values will be added

System.Collections.Specialized.NameValueCollection addFrom

The collection that contains the values that will be added

System.Collections.Generic.List<System.String> includeKeys

List with the only keys that should be added. null = all keys in the addTo collection will be added

System.Collections.Generic.List<System.String> excludeKeys

List with the keys that not should be added. null = all keys in the addTo collection includeKeys will be added

Remarks

If includeKeys is null and lstDoNotAddKeys is null, all keys will be added If excludeKeys has keys and lstDoNotAddKeys has keys, all keys in includeKeys that is not contained in excludeKeys will be added

AppendQuerystring(Uri, Uri)

Append the query strings from two URIs.

Declaration
public static string AppendQuerystring(Uri firstUri, Uri secondUri)
Parameters
Type Name Description
System.Uri firstUri

The first URI.

System.Uri secondUri

The second URI.

Returns
Type Description
System.String

An appended query string without leading '?', but with appropriate URL-encoding

BuildQueryString(NameValueCollection)

Make a query string from a collection, no leading '?'

Declaration
public static string BuildQueryString(NameValueCollection nvc)
Parameters
Type Name Description
System.Collections.Specialized.NameValueCollection nvc

The collection containing the query data.

Returns
Type Description
System.String

The query string with URL encoding, or an empty string (not null)

BuildQueryString(NameValueCollection, Encoding)

Builds a query string from a collection, no leading '?'

Declaration
public static string BuildQueryString(NameValueCollection nvc, Encoding encoding)
Parameters
Type Name Description
System.Collections.Specialized.NameValueCollection nvc

The collection containing the query data.

System.Text.Encoding encoding

The encoding to use.

Returns
Type Description
System.String

The query string with URL encoding, or an empty string (not null)

BuildQueryStringQ(NameValueCollection)

Make a query string from a collection, with leading '?'

Declaration
public static string BuildQueryStringQ(NameValueCollection nvc)
Parameters
Type Name Description
System.Collections.Specialized.NameValueCollection nvc
Returns
Type Description
System.String

The query string with URL encoding and leading '?', or an empty string (not null)

BuildUrlWithPageReference(String, ContentReference)

Helper method to construct a URL with a content reference parameter

Declaration
public static string BuildUrlWithPageReference(string url, ContentReference contentLink)
Parameters
Type Name Description
System.String url

The original URL

ContentReference contentLink

Content that should be referenced by the URL

Returns
Type Description
System.String

A new URL

Remarks

This method is primarily intended to be used with a URL read from the "PageURL" property of a PageData class. In some special cases you may need to retarget the URL to another page or another version of the same page.

Clear()

Resets cached values.

Declaration
public static void Clear()
Remarks

For performance reasons some configuration values are cached in the UriSupport class (UIUrl and UtilUrl). If the corresponding configuration values are updated without an application restart you should call this method to make sure that the new values are used.

Note: During normal circumstances you should never need to call this method.

Combine(String, String)

Combine two HTTP-scheme URLs or paths. Use for simple and robust concatentation of slash-separated paths, even if part of a full http: URL.

Declaration
public static string Combine(string s1, string s2)
Parameters
Type Name Description
System.String s1

First part, may include scheme, host etc

System.String s2

Second part, may include traling query string, fragment etc

Returns
Type Description
System.String

The correctly combined paths regardless of if s1 ends or not, or s2 starts or not, with "/"

Combine(Uri, Uri)

Combines the specified site root with a URL that can be absolute or application relative.

Declaration
public static Uri Combine(Uri siteRoot, Uri absoluteOrSiteRootRelative)
Parameters
Type Name Description
System.Uri siteRoot

The site root.

System.Uri absoluteOrSiteRootRelative

The absolute or site root relative url.

Returns
Type Description
System.Uri

The combined Uri

CreateAbsoluteUri(Url)

Creates an absolute URI from a Url instance.

Declaration
public static Uri CreateAbsoluteUri(Url url)
Parameters
Type Name Description
Url url

The URL.

Returns
Type Description
System.Uri

An absolute System.Uri for the Url.

EscapeUriSegments(String)

Converts all characters, except RFC 2396 reserved except '?' and unreserved characters, to their hexadecimal representation. This is equivalent to escaping all but RFC 2396 pchar characters in an abs_path. Use this method to escape paths in URI's. All Unicode characters are converted to UTF-8 format before being escaped. There must be no escaped characters in the string.

Declaration
public static string EscapeUriSegments(string s)
Parameters
Type Name Description
System.String s

The string to escape

Returns
Type Description
System.String

The encoded representation

Remarks

This method differs from Uri.EscapeUriString in that it also encodes '#', which although documented as doing, Uri.EscapeUriString does not. This is either a code-defect or a documentation issue in .NET framework 2.0. It also differs in that it encodes '?' and '&', wich Uri.EscapeUriString does not claim to do, nor does. Note that this method does not allow the use of '/' in the separate segments. Use System.Uri.UnescapeDataString to get the original string back.

GetEnterpriseUrlSettings(UrlBuilder)

Determines whether the specified URL refers to any known configured enterprise sites, and returns the appropriate settings if so.

Declaration
[Obsolete("Use GetSiteDefinition instead ")]
public static Settings GetEnterpriseUrlSettings(UrlBuilder url)
Parameters
Type Name Description
UrlBuilder url

The URL.

Returns
Type Description
Settings

The settings for the enterprise section, or null

Remarks

The logic of the method is to look at the sitehosts sections and the SiteUrl attributes for the sites. If any of those matches the host of the url that configuration is returned.

GetSiteDefinition(UrlBuilder)

Determines whether the specified URL refers to any known configured enterprise sites, and returns the appropriate SiteDefinition if so.

Declaration
public static SiteDefinition GetSiteDefinition(UrlBuilder url)
Parameters
Type Name Description
UrlBuilder url
Returns
Type Description
SiteDefinition

IsDataUri(String)

Determines whether the provided URI string is a data URI, e.g. data:image;base64,ZWF0IG15IHNob3J0cyE=.

Declaration
public static bool IsDataUri(string uri)
Parameters
Type Name Description
System.String uri

The URI string to test.

Returns
Type Description
System.Boolean

True if the string follows a data URI pattern; otherwise false.

IsSchemeSpecified(String)

Checks if the provided string represents an absolute URI (begins with scheme)

Declaration
public static bool IsSchemeSpecified(string uri)
Parameters
Type Name Description
System.String uri

String representing the URI

Returns
Type Description
System.Boolean

IsSiteUrlByRequestOrSettings(String)

Determines whether an URL is local by comparing with current request or settings

Declaration
public static bool IsSiteUrlByRequestOrSettings(string url)
Parameters
Type Name Description
System.String url

The URL.

Returns
Type Description
System.Boolean

true if the specified URL is URL local by request or settings; otherwise, false.

IsSiteUrlByRequestOrSettings(Uri)

Determines whether an URL is local by comparing with current request or settings

Declaration
public static bool IsSiteUrlByRequestOrSettings(Uri url)
Parameters
Type Name Description
System.Uri url

The URL.

Returns
Type Description
System.Boolean

true if the specified URL is URL local by request or settings; otherwise, false.

RemoveQueryStringDelimeter(String)

Since the Framework can't decide on if to include the delimiter (?) or not, this little helper will remove it when necessary.

Declaration
public static string RemoveQueryStringDelimeter(string query)
Parameters
Type Name Description
System.String query

null, empty, or string with or without delimiter '?'

Returns
Type Description
System.String

null, empty or string without delimiter '?'

ResolveUrlBySettings(String)

Resolves the application relative path, using configuration settings.

Declaration
public static string ResolveUrlBySettings(string path)
Parameters
Type Name Description
System.String path

The path.

Returns
Type Description
System.String

A root-relative path

Remarks

Handles both relative and absolute URI's.

ResolveUrlFromUIAsRelativeOrAbsolute(String)

Resolves the URL from UI as relative or absolute.

Declaration
public static string ResolveUrlFromUIAsRelativeOrAbsolute(string path)
Parameters
Type Name Description
System.String path

The path.

Returns
Type Description
System.String

A string with the path resolved either as an absolute URL (with scheme, host etc) if the UIUrl is an absolute URL, or as a rooted, relative path (starting with /) if the UIUrl is not an absolute URL.

ResolveUrlFromUIBySettings(String)

Resolves the path relative the UI directory.

Declaration
public static string ResolveUrlFromUIBySettings(string path)
Parameters
Type Name Description
System.String path

The path.

Returns
Type Description
System.String

A root-relative path

ResolveUrlFromUtilBySettings(String)

Resolves the path relative the Util directory.

Declaration
public static string ResolveUrlFromUtilBySettings(string path)
Parameters
Type Name Description
System.String path

The path.

Returns
Type Description
System.String

A root-relative path

Split(String)

Splits the specified escaped URL into three parts: [0] - The [scheme][host][path] portion - before any fragment or query [1] - The query, if any, including the '?' [2] - The fragment, if any, including the '#'

Declaration
public static string[] Split(string url)
Parameters
Type Name Description
System.String url

The URL.

Returns
Type Description
System.String[]

A string[3] array with the parts, or empty strings if no such part

SplitNoDelim(String)

Splits the specified escaped URL into three parts: [0] - The [scheme][host][path] portion - before any fragment or query [1] - The query, if any, excluding the '?' [2] - The fragment, if any, excluding the '#'

Declaration
public static string[] SplitNoDelim(string url)
Parameters
Type Name Description
System.String url

The URL.

Returns
Type Description
System.String[]

A string[3] array with the parts, or empty strings if no such part

Extension Methods