views:

329

answers:

6

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!

A: 

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
A: 

Update:
How about this:

Regex.Split(url, "/");
Lirik
but he only wants to split on one character, not both.
mmr
The question states that it _shouldn't_ split on a \.
bdukes
+1  A: 

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
+7  A: 

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
I agree. I think that the url isn't a string.
mmr
I get the same. I suspect url is not what OP says it is.
Austin Salonen
A: 

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
A: 

those two functions are probably converting \ to / because \ is not a valid character in a URL (see http://stackoverflow.com/questions/1547899/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.

Charles Boyung
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.
Sheep Slapper
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