36

I need to URL encode some periods since I have to pass some document path along and it is like this

http://example.com/test.aspx?document=test.docx

So test.docx is causing me an error of an illegal character. So I need to change it to

.  -->  %2E

I tried to use Server.UrlEncode

  string b = Server.UrlEncode("http://example.com/test.aspx?document=test.docx");

but I get

"http%3a%2f%2fexample.com%2ftest.aspx%3fdocument%3dtest.docx"

So do I have to use like a string replace and do it manually and replace all periods with that code?

jww
  • 97,681
  • 90
  • 411
  • 885
chobo2
  • 83,322
  • 195
  • 530
  • 832

2 Answers2

55

This is a really old question, but I ran into this searching for a similar problem. I stuck a "/" onto the end of my url's with periods in them and it got around the problem.

Andrew Edvalson
  • 7,658
  • 5
  • 26
  • 24
  • 2
    Just a note for anyone who will utilize this answer. This will work for _most_ use cases, including the one OP provided. This does **not** work if the URL ends with a dot character. – Ben May 10 '17 at 16:09
  • good solution but if a user reloads the page he gets 404 again in Angular – Toolkit Jun 03 '17 at 12:58
23

The period there isn't he problem (given that %2E doesn't solve the problem). A period is a perfectly valid URL character whatever the problem is it's not the period. Check the stack trace of the error being throw or post the complete error details.

And you shouldn't be URL encoding the entire path. Only the query string parameter value.

string b = "http://example.com/test.aspx?document=" + Server.UrlEncode("test.docx");

Are you still getting the error if you try it that way?

I wouldn't touch SharePoint with a ten foot pole. However, escaping the period wouldn't necessarily stop SharePoint from doing it's shenanigans. But I guess you should at least try it.

Server.UrlEncode("test.docx").Replace(".", "%2E");
John Leidegren
  • 59,920
  • 20
  • 131
  • 152
  • 2
    Well sharepoint does not like it. I think in the query string or whatever it is using part of the url path to find the root folder and that cannot contain a period. %2E from limit testing seems to fix it. – chobo2 May 31 '11 at 17:50
  • 1
    I ran into a similar problem when using ASP.NET Web Pages. Example: http://www.cutrategamer.com/app/remove-subscription/30/myemail%40gmail.com would throw an error message. Will try replacing period with "%2E". – Tod Birdsall May 11 '12 at 05:42
  • 2
    Any luck, @Tod1d ? I'm having the same problem with a very similar ASP.NET Web API url. – Tim Franklin Jul 11 '13 at 19:46
  • 1
    @TimFranklin and others with the same problem, see Andrew's answer below. -- The trailing slash seems to fix it. -- This is a very silly bug IMO. – BrainSlugs83 Jan 26 '17 at 02:55