Micah
Micah

Reputation: 116170

How can I pass a url as a variable in a querystring to my controller?

I'm trying pass in a url as a paramter to my controller like this:

//Passed in via url like this: 
//http://www.mydomain.com/Puzzle/ContinuePuzzle/{insert url here}
public ActionResult ContinuePuzzle(string url)
{
    return View("PuzzleWrapper", 
                 (object)_PuzzleService.ContinuePuzzle(url);
}

Whenever I try this I get a http 400 Bad Request error. I've tried UrlEncoding it, but it still doesn't like it. Any suggestions?

Upvotes: 0

Views: 299

Answers (3)

Dale Ragan
Dale Ragan

Reputation: 18270

I have a suspicion having two scheme declarations in the URL is causing the default route handler to throw the 400 response.

As a work around, I would just send the URL without the scheme (i.e. http://). I tested this scenario without any problems:

http://www.mydomain.com/Puzzle/ContinuePuzzle/www.domain.com/nextpuzzle

Using this route:

routes.MapRoute("Puzzle", "Puzzle/ContinuePuzzle/{*url}", new {controller = "Puzzle", action = "ContinuePuzzle", url = ""});

If you have to have the scheme, then use a querystring parameter like Charlino suggested.

Upvotes: 3

ten5peed
ten5peed

Reputation: 15890

UrlEncode it but pass it as a get variable...?

E.g. http://www.mydomain.com/Puzzle/ContinuePuzzle?url=http%3a%2f%2fmypuzzlingdomain.com%2f%3fa4

Upvotes: -1

Brettski
Brettski

Reputation: 20101

What if you put it in parenthesis?

To have http:// start again on the url is an invalid address.

http://www.mydomain.com/Puzzle/ContinuePuzzle/(http://mypuzzlingdomain.com/?a4)

Or perhaps braces may work also.

Upvotes: 0

Related Questions