1

I have a situation where I need to put a url inside a GET variable.
for example:

'http://www.site.com/StepOne?b=1&afterUrl=http://www.site.com/StepTwo?someVar=text&b=1'

In this case, when I am at StepOne the b param value will be 1,1 and not 1 as expected. the afterUrl param will be:

'http://www.site.com/StepTwo?someVar=text'

instead of this:

'http://www.site.com/StepTwo?someVar=text&b=1'

How do I isolate the afterUrl param so its own GET variables won't effect the entire URL.

ToddBFisher
  • 11,370
  • 8
  • 38
  • 54
SexyMF
  • 10,657
  • 33
  • 102
  • 206

4 Answers4

2

When you are creating the afterUrl URL parameter, be sure to UrlEncode() the value.

e.g.

var url = String.Format("http://www.site.com/StepOne?b={0}&afterUrl={1}", b, Server.UrlEncode(afterUrl));
GregL
  • 37,147
  • 8
  • 62
  • 67
1

Consider using HttpUtility.UrlEncode() for the AfterURL

(EDIT or Server.URLEncode() as others have pointed out)

"http://www.site.com/StepOne?b=1&afterUrl=" + 
HttpUtility.UrlEncode(http://www.site.com/StepTwo?someVar=text&b=1");

Then when you finally hit the "StepOne" page you can use HttpUtility.UrlDecode(AfterURL variable name). From there you can Response.redirect or whatever you want with the preserved after url.

ToddBFisher
  • 11,370
  • 8
  • 38
  • 54
0

Use Server.UrlEncode on someVar to escape out it's querystring values before putting it in the link. You may need to use Server.UrlDecode on the other side to convert it back to the original characters.

pantryfight
  • 338
  • 1
  • 3
  • 13
0

Quite simply, you need to URL encode the afterUrl param (actually, you should URL encode all parameters passed to a server), which will turn "http://www.site.com/StepTwo?someVar=text&b=1" into "http%3A%2F%2Fwww.site.com%2FStepTwo%3FsomeVar%3Dtext%26b%3D1", which won't affect the set of parameters. Almost any server framework on the market will automatically decode that back into the string "http://www.site.com/StepTwo?someVar=text&b=1", or at least give you a function to do so yourself.

EDIT:

As this SO question shows, it is possible to URL encode a string without using System.Web, using System.Net.Uri.EscapeDataString().

Community
  • 1
  • 1
Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52