Kevin
Kevin

Reputation: 413

How exactly does Url Encoding work?

In MVC, I'm attempting to use URL routing to get the result of an action given a certain input.

Consider the following in my view:

<%=Html.ActionLink("View", "Test", new with {.id = Url.Encode(dir\file}) %>

My controller then uses HttpUtility.UrlDecode(id) to get the original. The controller itself is using File() to retrieve a file at the specified directory\file location. However, an error message pops up telling me that

A potentially dangerous Request.Path value was detected from the client (%).

The URL is showing up as

http://home/dir%255cfile.txt

I googled Url Encoding and \ is encoded as %5c. Where is the %25 coming from? It's the encoding for %, but that means Encode is being done twice. Why is that, and is that supposed to be happening?

Upvotes: 2

Views: 422

Answers (1)

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29965

Html.ActionLink takes care of the URL encoding for you. If you don't encode the params there, there's no need to decode it again and your issue is solved.

Upvotes: 1

Related Questions