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?

4
  • Well, you're url-encoding your url. Don't do that, and it'll work as a url. =) Commented Jul 22, 2013 at 14:52
  • I don't understand what you mean. I need to encode it for other reasons Commented Jul 22, 2013 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. Commented Jul 22, 2013 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... Commented Jul 22, 2013 at 14:59

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.