3

I've got a .NET 3.5 web application written in C# doing some URL rewriting that includes a file path, and I'm running into a problem. When I call string.Split('/') it matches both '/' and '\' characters. Is that... supposed to happen? I assumed that it would notice that the ASCII values were different and skip it, but it appears that I'm wrong.

// url = 'someserver.com/user/token/files\subdir\file.jpg
string[] buffer = url.Split('/');

The above code gives a string[] with 6 elements in it... which seems counter intuitive. Is there a way to force Split() to match ONLY the forward slash? Right now I'm lucky, since the offending slashes are at the end of the URL, I can just concatenate the rest of the elements in the string[], but it's a lot of work for what we're doing, and not a great solution to the underlying problem.

Anyone run into this before? Have a simple answer? I appreciate it!

More Code:

url = HttpContext.Current.Request.Path.Replace("http://", "");
string[] buffer = url.Split('/');

Turns out, Request.Path and Request.RawUrl are both changing my slashes, which is ridiculous. So, time to research that a bit more and figure out how to get the URL from a function that doesn't break my formatting. Thanks everyone for playing along with my insanity, sorry it was a misleading question!

Stephen Fischer
  • 2,445
  • 2
  • 23
  • 38
  • is url a string, a uri, or a path? Because on windows, in a path, I think that the '/' and '\' characters can be used interchangeably, but only sometimes. – mmr Apr 05 '10 at 18:22
  • I have attempted to reproduce your problem, and I haven't been able to. Can you create a short but complete program that demonstrates the problem? – Jeffrey L Whitledge Apr 05 '10 at 18:30

6 Answers6

7

When I try the following:

string url = @"someserver.com/user/token/files\subdir\file.jpg";
string[] buffer = url.Split('/');
Console.WriteLine(buffer.Length);

... I get 4. Post more code.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
1

Something else is happening, paste more code.

string str = "a\\b/c\\d";
string[] ts = str.Split('/');
foreach (string t in ts)
{
    Console.WriteLine(t);
}

outputs

a\b
c\d

just like it should. My guess is that you are converting / into \ somewhere.

MK.
  • 33,605
  • 18
  • 74
  • 111
0

You could use regex to convert all \ slashes to a temp char, split on /, then regex the temp chars back to \. Pain in the butt, but one option.

Catdirt
  • 96
  • 5
0

I suspect (without seeing your whole application) that the problem lies in the semantics of path delimiters in URLs. It sounds like you are trying to attach a semantic value to backslashes within your application that is contrary to the way HTTP protocols define and use backslashes.

This is just a guess, of course.

The best way to solve this problem might be modifying the application to encode the path in some other way (such as "%5C" for backslashes, maybe?).

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99
0

those two functions are probably converting \ to / because \ is not a valid character in a URL (see Which characters make a URL invalid?). The browser (NOT C#, as you are inferring) is assuming that when you are using that invalid character, you mean /, so it is "fixing" it for you. If you want \ in your URL, you need to encode it first.

The browsers themselves are actually the ones that make that change in the request, even if it is behind the scenes. To verify this, just turn on fiddler and look at the URLs that are actually getting sent when you go to a URL like this. IE and Chrome actually change the \ to / in the URL field on the browser itself, FireFox doesn't, but the request goes through that way anyways.

Community
  • 1
  • 1
Charles Boyung
  • 2,464
  • 1
  • 21
  • 31
  • Even after encoding it to %5C (which was my first instinct), the problem persisted. And watching the headers being sent across, the slashes were NOT converted in the URL then (in Firefox, at least). So again, not sure how they're being converted even when encoded, but it's kind of a non issue now. – Stephen Fischer Apr 06 '10 at 19:26
  • That's weird with Firefox, because it did when I tried it. And because other browsers definitely do convert to /, you wouldn't want to separate on that character anyways. As for the error still occurring when using the encoded character, there must be something with WHEN you were doing this, because I ran a test and it worked fine. – Charles Boyung Apr 08 '10 at 15:51
-1

Update:
How about this:

Regex.Split(url, "/");
Kiril
  • 39,672
  • 31
  • 167
  • 226