0

Needed help again

So I have this code

<a href="/Categories.aspx?categoryName=Electronics & Gadgets" class="clearfix">                                                                      

How to make sure that the requestString will retrieve everything from "Electronics & Gadgets" and not just "Electronics"?

Thank you

iDeal
  • 201
  • 2
  • 4
  • 7
  • You need to url encode the value "Electronics & Gadgets" – Chetan Jul 27 '17 at 00:59
  • See [UrlEncode](https://msdn.microsoft.com/en-us/library/4fkewx0t(v=vs.110).aspx) and [this](https://stackoverflow.com/questions/1517586/how-do-i-replace-all-the-spaces-with-20-in-c-sharp) – Jasen Jul 27 '17 at 01:05
  • @ChetanRanpariya how to do that? Thanks – iDeal Jul 27 '17 at 02:47

1 Answers1

6

You should write something like

<a href="<%= "/Categories.aspx?categoryName=" + HttpUtility.UrlEncode("Electronics & Gadgets") %>" class="clearfix">Link</a>

as the query string value is not a valid url format.

In this case,

the space " " and ampersand "&" characters will be encoded to "+" and "%26" respectively.

** Note that the ampersand character was used to concatenate multiple query strings so it must be properly encoded.

Threfore the url formatted link would be

/Categories.aspx?categoryName=Electronics+%26+Gadgets

You'll then use HttpUtility.UrlDecode to parse the query string value in your code behind.


References:-

MSDN HttpUtility.HtmlEncode

MSDN HttpUtility.HtmlDecode

Zephyr
  • 314
  • 2
  • 8