Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.64 KB | None | 0 0
  1. #Region " Imports "
  2.  
  3. Imports System.ComponentModel
  4. Imports System.Runtime.CompilerServices
  5. Imports System.Text
  6. Imports System.Web
  7.  
  8. #End Region
  9.  
  10. #Region " NameValueCollection Extensions "
  11.  
  12. Namespace ElektroKit.Core.Extensions.[NameValueCollection]
  13.  
  14.     ''' <summary>Contains custom extension methods to use with an <see cref="Collections.Specialized.NameValueCollection"/>.</summary>
  15.     <HideModuleName>
  16.     Public Module NameValueCollectionExtensions
  17.  
  18.         <DebuggerStepThrough>
  19.         <Extension>
  20.         <EditorBrowsable(EditorBrowsableState.Always)>
  21.         Public Function ToQueryString(sender As Collections.Specialized.NameValueCollection,
  22.                                       baseAddress As Uri) As String
  23.  
  24.             Return NameValueCollectionExtensions.ToQueryString(sender, baseAddress.AbsoluteUri)
  25.  
  26.         End Function
  27.  
  28.         <DebuggerStepThrough>
  29.         <Extension>
  30.         <EditorBrowsable(EditorBrowsableState.Always)>
  31.         Public Function ToQueryString(sender As Collections.Specialized.NameValueCollection,
  32.                                       baseAddress As String) As String
  33.  
  34.             Dim sb As New StringBuilder
  35.             If Not String.IsNullOrWhiteSpace(baseAddress) Then
  36.                 sb.Append(baseAddress.TrimEnd({"?"c}))
  37.                 sb.Append("?")
  38.             End If
  39.  
  40.             For Each key As String In sender.AllKeys
  41.                 sb.AppendFormat("{0}={1}&", key, HttpUtility.UrlEncode(sender(key)))
  42.             Next
  43.  
  44.             Return sb.Remove((sb.Length - 1), 1).ToString() ' removes the last "&" char.
  45.  
  46.         End Function
  47.  
  48.     End Module
  49.  
  50. End Namespace
  51.  
  52. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement