4

On ASP.NET MVC 5.1 I have an action which receives an encrypted string, for example:

Nf7JnWp/QXfA9MNd52RxKpWg=

But I get a 404 error because of the slash inside this string ...

I tried to encode the string with HttpUtility.UrlEncode and WebUtility.UrlEncode;

But I keep having the same problems. Does anyone knows how to solve this?

Thank You,

Miguel

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • So if you pass a string without a `/`, do you get routed to the right action or do you get a 404? – Mister Epic Mar 11 '14 at 11:35
  • I get routed to the right action ... – Miguel Moura Mar 11 '14 at 11:42
  • Can you provide the rendered link from your source? – Mister Epic Mar 11 '14 at 11:44
  • Yes, I tried the following @Html.ActionLink("Test", MVC.User.Remove(WebUtility.UrlEncode("L22llNf7JnWp/QXfA9MNd52RxKpWg="))) and the rendered url is "localhost:8580/remover/l22llnf7jnwp%252fqxfa9mnd52rxkpwg%253d" and I get the error "A potentially dangerous Request.Path value was detected from the client (%)." If I type it to the url bar I get a 404 error – Miguel Moura Mar 11 '14 at 11:56
  • Instead of passing it via a URL segement, can you try a querystring (using the name of your action parameter: localhost:8580/remover?ID=l22llnf7jnwp%252fqxfa9mnd52rxkpwg%253d – Mister Epic Mar 11 '14 at 12:01

1 Answers1

2

You can build a workaround for this by defining a custom route. Now I do not know how you named your controller or your action, so I'll use generic names.

routes.MapRoute(
    "SpecialControllerName",
    "CustomName/{*id}",
     new { controller = "CustomName", action = "CustomAction", id = UrlParameter.Optional }
);

public ActionResult Name(string id)
{
  //logic goes here

}

So what we did here, is take the action out of the equation. Now if you call http://yourdomain.com/CustomName/Nf7JnWp/QXfA9MNd52RxKpWg= it will call the Action method CustomName in the Controller CustomNameController.

Please note, that the asp.net Framework takes the first route in your route config, which matches its patterns. If you have your Defaultroute and place your new custom route below it, it will fail. Placing the Custom route above it, will work

Similar questions on SO:

  1. ActionLink contains slash ('/') and breaks link

  2. URLs with slash in parameter?

Community
  • 1
  • 1
Marco
  • 22,856
  • 9
  • 75
  • 124