0

www.yoursite.com/image/http://images.google.com.ph/images/nav_logo7.png

What I need to know here is the Controller Action, and the Global.asax routes

tereško
  • 58,060
  • 25
  • 98
  • 150
h3n
  • 5,142
  • 9
  • 46
  • 76

4 Answers4

8

The colon : character is not valid in the path segment of the URL, so you'll have to either encode it, or remove it entirely. After that, you can use the {*routeValue} syntax to specify that the route value should be assigned the remainder of the URL.

routes.MapRoute( 
    "Image", 
    "image/{*url}", 
    new { controller = "Image", action = "Index" } 
); 

For the url http://www.yoursite.com/image/images.google.com.ph/images/nav_logo7.png , the above route will execute ImageController.Index() with a url argument of "images.google.com.ph/images/nav_logo7.png". How you choose to deal with the protocol (encode/remove) is up to you.

Also keep in mind that a url authority can be made up of a domain name and a port number, separated by : (www.google.com:80) which would also need to be encoded.

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • `:` is not your only problem, there are many more reserved characters like `! * ' ( ) ; : @ & = + $ , / ? % # [ ]`. How do you guarantee they are not in the URL? You should use a full UrlEncode over entire URL. You can then convert %2F back to `/` if it needs to be preety – TFD Feb 02 '10 at 23:44
  • My original answer assumed that the requirement dealt with URIs that were already deemed valid. As such, no additional encoding would be required. – Richard Szalay Feb 03 '10 at 07:45
5

If you want to send a URL as a parameter on a URL you need to URL Encode it first

In c# use Server.UrlEncode(string) from the System.Web namespace

So your example will look like:

www.yoursite.com/image/http%3a%2f%2fimages.google.com.ph%2fimages%2fnav_logo7.png

And your route pattern could be:

routes.MapRoute(
    "image",
    "image/{url}",
    new { controller = "Image", action = "Index", url = "" }
);
TFD
  • 23,890
  • 2
  • 34
  • 51
1

I'd start by not trying to embed a second URL into your route.

In cases where I have to use a URL as part of a route, I replace the slashes with an alternate character so you don't have issues with the interpertation of the URL as a malformed route (i.e.~,|,etc.) then retranslate these with some string replaces in the controller. And if possible, I'd ditch the HTTP:// and assume the route is a URL by convention.

So your route would become something like:

www.yoursite.com/image/images.google.com.ph~images~nav_logo7.png

Bob Palmer
  • 4,714
  • 2
  • 27
  • 31
0

URL-encoded slash in URL - Stack Overflow

This is same problem and solved solutions.

1st solution. Replace "://" to "/". Routing url pattern "image/{scheme}/{port}/{*url}".

2nd solution "image/{*url}" set *url value base64.

Community
  • 1
  • 1
takepara
  • 10,413
  • 3
  • 34
  • 31