/// <summary> /// Encodes text for inclusion in a URI. /// </summary> /// <param name="text">The text to encode for inclusion in a URI.</param> /// <param name="uriPart">A <see cref="UriPart"/> value indicating where in the URI the specified text will be included.</param> /// <param name="encoding">The encoding to use for Unicode characters in the URI. If this value is <see langword="null"/>, the <see cref="Encoding.UTF8"/> encoding will be used.</param> /// <returns>The URI-encoded text, suitable for the specified URI part.</returns> /// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">If <paramref name="uriPart"/> is not a valid <see cref="UriPart"/>.</exception> public static string UriEncode(string text, UriPart uriPart, Encoding encoding) { if (text == null) { throw new ArgumentNullException("text"); } encoding = encoding ?? Encoding.UTF8; switch (uriPart) { case UriPart.Any: return(UriEncodeAny(text, encoding)); case UriPart.AnyUrl: return(UriEncodeAnyUrl(text, encoding)); case UriPart.Host: return(UriEncodeHost(text, encoding)); case UriPart.Path: return(UriEncodePath(text, encoding)); case UriPart.PathSegment: return(UriEncodePathSegment(text, encoding)); case UriPart.Query: return(UriEncodeQuery(text, encoding)); case UriPart.QueryValue: return(UriEncodeQueryValue(text, encoding)); case UriPart.Fragment: return(UriEncodeFragment(text, encoding)); default: throw new ArgumentException("The specified uriPart is not valid.", "uriPart"); } }
/// <summary> /// Encodes text for inclusion in a URI. /// </summary> /// <param name="text">The text to encode for inclusion in a URI.</param> /// <param name="uriPart">A <see cref="UriPart"/> value indicating where in the URI the specified text will be included.</param> /// <param name="encoding">The encoding to use for Unicode characters in the URI. If this value is <see langword="null"/>, the <see cref="Encoding.UTF8"/> encoding will be used.</param> /// <returns>The URI-encoded text, suitable for the specified URI part.</returns> /// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">If <paramref name="uriPart"/> is not a valid <see cref="UriPart"/>.</exception> public static string UriEncode(string text, UriPart uriPart, Encoding encoding) { if (text == null) throw new ArgumentNullException("text"); encoding = encoding ?? Encoding.UTF8; switch (uriPart) { case UriPart.Any: return UriEncodeAny(text, encoding); case UriPart.AnyUrl: return UriEncodeAnyUrl(text, encoding); case UriPart.Host: return UriEncodeHost(text, encoding); case UriPart.Path: return UriEncodePath(text, encoding); case UriPart.PathSegment: return UriEncodePathSegment(text, encoding); case UriPart.Query: return UriEncodeQuery(text, encoding); case UriPart.QueryValue: return UriEncodeQueryValue(text, encoding); case UriPart.Fragment: return UriEncodeFragment(text, encoding); default: throw new ArgumentException("The specified uriPart is not valid.", "uriPart"); } }
/// <summary> /// Encodes text for inclusion in a URI using the <see cref="Encoding.UTF8"/> encoding. /// </summary> /// <remarks> /// This method calls <see cref="UriEncode(string, UriPart, Encoding)"/> using the default /// <see cref="Encoding.UTF8"/> encoding. /// </remarks> /// <param name="text">The text to encode for inclusion in a URI.</param> /// <param name="uriPart">A <see cref="UriPart"/> value indicating where in the URI the specified text will be included.</param> /// <returns>The URI-encoded text, suitable for the specified URI part.</returns> /// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">If <paramref name="uriPart"/> is not a valid <see cref="UriPart"/>.</exception> public static string UriEncode(string text, UriPart uriPart) { return UriEncode(text, uriPart, Encoding.UTF8); }
/// <summary> /// Encodes text for inclusion in a URI using the <see cref="Encoding.UTF8"/> encoding. /// </summary> /// <remarks> /// <para>This method calls <see cref="UriEncode(string, UriPart, Encoding)"/> using the default /// <see cref="Encoding.UTF8"/> encoding.</para> /// </remarks> /// <param name="text">The text to encode for inclusion in a URI.</param> /// <param name="uriPart">A <see cref="UriPart"/> value indicating where in the URI the specified text will be included.</param> /// <returns>The URI-encoded text, suitable for the specified URI part.</returns> /// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">If <paramref name="uriPart"/> is not a valid <see cref="UriPart"/>.</exception> public static string UriEncode(string text, UriPart uriPart) { return(UriEncode(text, uriPart, Encoding.UTF8)); }