0

When I use HttpUtility.UrlEncode to encode a Url I end up getting a server error.

ASP.Net code:

NavigateUrl=<%# HttpUtility.UrlEncode(string.Concat("UpdateMember.aspx","?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString())) %> 

Url:

http://localhost/UITest/MM/UpdateMember.aspx%3fgroupId%3d0032409901

which results in "HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."

However using:

NavigateUrl=<%# string.Concat("UpdateMember.aspx","?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString()) %> 

results in the Url:

http://localhost/UITest/MM/UpdateMember.aspx?groupId=0032409901

which works out fine. Am I doing something incorrectly?

Sperick
  • 2,691
  • 6
  • 30
  • 55
  • Well, you're url-encoding your url. Don't do that, and it'll work as a url. =) – J. Steen Jul 22 '13 at 14:52
  • I don't understand what you mean. I need to encode it for other reasons – Sperick Jul 22 '13 at 14:53
  • 1
    Url-encoding is used to pass values in a url, that might otherwise be perceived as part of the url. So, ehr. Don't do that. Only encode the part of the url that you need to encode. – J. Steen Jul 22 '13 at 14:55
  • There are valid reasons for wanting to URL-encoude a URL but the example you gave makes one think that in this scenario, you don't need to... – Icarus Jul 22 '13 at 14:59

2 Answers2

1

You shouldn't encode the entire URL, atleast not the 1st "?" symbol. If you encode the ? too then your application looks for a file with the name & extension "UpdateMember.aspx%3fgroupId%3d0032409901" which doesn't exist.

Probably, this is what you should do.

http://localhost/UITest/MM/UpdateMember.aspx?groupId%3d0032409901
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
  • I think this is partly right. You should also leave the = that is between the name and the value untouched. UrlEncode all values and not the names (if you had more than one name/value pair on your Url) – Richard Jul 22 '13 at 15:31
0

HttpUtility.UrlEncode() URL-encodes a string

That means that it escapes all special characters from the string so that you can insert it as part of a URL without any characters being parsed as URL modifiers.

You use this kind of escape function when inserting arbitary text as part of a URL.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964