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

asked Mar 11, 2014 at 11:25
5
  • So if you pass a string without a /, do you get routed to the right action or do you get a 404? Commented Mar 11, 2014 at 11:35
  • I get routed to the right action ... Commented Mar 11, 2014 at 11:42
  • Can you provide the rendered link from your source? Commented Mar 11, 2014 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 Commented Mar 11, 2014 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 Commented Mar 11, 2014 at 12:01

1 Answer 1

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?

answered Mar 11, 2014 at 13:45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.