Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
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 Answers 2

up vote 1 down vote accepted

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
share|improve this answer
    
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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.