How do you use an Ampersand in an HTTPCookie in VB.NET?

后端 未结 3 1047
挽巷
挽巷 2021-01-27 01:14

I have a cookie saved to the user as follows...

Dim searchCookie As HttpCookie = New HttpCookie(\"SearchCriteria\")
searchCookie.Item(\"SearchText\") = FullSearc         


        
相关标签:
3条回答
  • 2021-01-27 01:27

    D'oh! I'm such a dork...

    Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
    searchCookie.Item("SearchText") = HttpContext.Current.Server.UrlEncode(FullSearchCriteria.SearchText)
    searchCookie.Item("SearchType") = HttpContext.Current.Server.UrlEncode(FullSearchCriteria.SearchType)
    
    0 讨论(0)
  • 2021-01-27 01:36

    The cookie values need to be encoded. I'm no VB expert, but it looks like this is done with the method

    System.Web.HttpUtility.UrlEncode
    
    0 讨论(0)
  • 2021-01-27 01:41

    You can use the URLEncode method.

    Something like:

    imports HttpContext.Current
    ...
    Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
    searchCookie.Item("SearchText") = Server.UrlEncode(FullSearchCriteria.SearchText)
    searchCookie.Item("SearchType") = Server.UrlEncode(FullSearchCriteria.SearchType)
    

    This is essential as only certain characters are allowed in cookies with characters such as ampersands breaking them.

    0 讨论(0)
提交回复
热议问题