0

I need to pass this filepath over via route to my actionmethod:

<p><a href="/Cars/@Model.CarId/@car.ContainerPath/@Model.UserId/Url">@car.Name</a></p>

so for example @car.ContainerPath is a string of "34_Creating%20Cars%20Forms/Exercise%20Cars/Audi%202010%20Parts%20Reference.pdf"

I need to escape this somehow I think? I would prefer not to send this over url but with a hyperlink I don't see a way not to.

UPDATE:

For additional info, here's the actionmethod it's going to:

    public string GetFileZipDownloadUrl(CarViewModel model, string fileContainerPath)
    {
        string downloadUrl = string.Empty;
        downloadUrl = GetFileZipDownloadUrl(model.CarId,fileContainerPath, model.UserId);

        return downloadUrl;
    }

so I'm sending over for that fileContainerPath paths like this in the url for that @car.ContainerPath param:

"55_Creating Cars Forms/Exercise Cars/Audi Parts Reference.pdf"

so the route url before it's requested looks like this when formed in that hyperlink:

http://Cars/55/55_Creating Cars Forms/Exercise Cars/Audi Parts Reference.pdf/20/Url

My action method just needs to use that path to go get a reference to a file under the hood.

PositiveGuy
  • 46,620
  • 110
  • 305
  • 471

2 Answers2

1

If you want to just get rid of %20 in the url use encoding/decoding like in @Xander's answer. However if any of your data is very dynamic and can have weird characters you should consider adding a Safe() and Unsafe() methods that will strip out all the "Dangerous" characters for url, and then turn it back to original value.

bobek
  • 8,003
  • 8
  • 39
  • 75
  • And then if any of your data like @car.ContainerPath has weird character you should make a Safe() method and use it like @car.ContainerPath.Safe() where safe will replace all the characters you don't want with for example dashes. – bobek Mar 29 '12 at 21:01
  • yea I'll implement the safe after I get this going but for sure...that's a great tip that if I hadn't experienced this before would have not thought of – PositiveGuy Mar 29 '12 at 21:02
  • I tried my previous comment but still get % in it and + now to concat. e.g http://localhost/Cars/55/55_Creating+Cars+Forms%2fExercise+Cars%2fAudi+2010+Parts+Reference.pdf/32/Url – PositiveGuy Mar 29 '12 at 21:03
  • side thought to my last reply: there's got to be a better way to pass this filepath than through querystring when using a hyperlink but a hyperlink does not post back so I think this is the only way in MVC not sure – PositiveGuy Mar 29 '12 at 21:06
  • http://stackoverflow.com/questions/591694/url-encoded-slash-in-url here. It's a percentage value, URLs shouldn't have those that's why it leaves it decoded. Use Safe() to replace %2f with something else – bobek Mar 29 '12 at 21:07
  • anyhow how would the action method determine when the filepath inside that url starts and ends! that's another question. Since the filepath param has slashes also, and so does the overall url, how does the action method know where the filepath start and end is for my @car.ContainerPath in the first place? – PositiveGuy Mar 30 '12 at 05:11
0

Raw Url:

HttpUtility.UrlEncode(rawurl);

Decode encoded url:

HttpUtility.UrlDecode(encodedurl);

http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx

http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode.aspx

Alex
  • 34,899
  • 5
  • 77
  • 90