1

Response.Redirect("./blah.aspx?key=my value with spaces");

It sends the browser to:

mysite/blah.aspx?key=my%20value%20with%20spaces

Now i understand why it's doing this - for the sake of ancient browsers that would choke on the spaces. But really, what i want is to have a nice-looking url with spaces instead of %'s everywhere, because it works just the same.

Is there some way to stop response.redirect urlencoding my spaces?

Thanks a lot

Chris
  • 39,719
  • 45
  • 189
  • 235
  • so you think a URL with spaces is "nice looking" ? i suggest you look at this SO question: http://stackoverflow.com/questions/497908/are-urls-allowed-to-have-a-space-in-them – RPM1984 Sep 20 '10 at 00:39
  • Had a look, it says "The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs." - i don't really plan on putting my URLS through a word processor, i think it'll be OK. – Chris Sep 20 '10 at 00:54
  • It doesn't matter what you plan to do with the URI. URIs are defined according to an international standard that has to expect it will be used for a variety of use cases (and word-processors are hardly the toughest case here, compared to space-separated lists, which are commonly used in some cases). Because there are plenty of cases where spaces simply don't work, spaces are not allowed. – Jon Hanna Sep 20 '10 at 08:48
  • Ok don't worry. I've gone with the spaces-replaced-with-dashes option. – Chris Sep 22 '10 at 04:27

2 Answers2

8

You can't have a valid URL with spaces, the space character is actually illegal in an URL.

You can't make the Response.Redirect method avoid encoding the spaces, it's not designed to create an illegal URL.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • +1. Spot on. The encoding is there for a REASON, why do people try and fight against it? – RPM1984 Sep 20 '10 at 00:51
  • I know, you're right, but this is an intranet app, i'm fine with breaking the rules a bit. Any ideas how to do it? – Chris Sep 20 '10 at 00:55
  • I don't think it's possible to do that, is it that important to have spaces in your URLs? why not just replace them with something else instead, spaces would look horrible in URLs anyways.. – bevacqua Sep 20 '10 at 01:00
  • exactly. just use underscores or hyphen. – RPM1984 Sep 20 '10 at 01:02
  • 1
    You could make you own Redirect method that breaks any rules you want. All it does really is to replace the current output with a redirection page, and call Response.End. – Guffa Sep 20 '10 at 02:44
3

you could always replace spaces in your key with scores before redirecting, and "decoding" them into spaces again after the fact like so:

string urlString = "./blah.aspx?key=my value with spaces";
Response.Redirect(urlString.Replace(' ','-'));

and on the page that grabs the querystring:

string queryKey = Request["key"].Replace('-',' ');

(be careful of nulls in Request["key"] here though)

bevacqua
  • 47,502
  • 56
  • 171
  • 285