1

In a C# MVC4 web project I have the following custom route:

routes.MapRoute( 
   name: "ProductDetails", 
   url: "Details/{productName}/{revision}",             
   defaults: new { controller = "Product", action = "Details", productName = "", revision = 1 });

Which points to a controller method:

public ActionResult Details(String productName, Int32? revision)
{ ... }

And I generate links like this:

<a href="@Url.Action("Details" ,"Product", new { productName = @prod.ProductName, revision = @prod.Revision })" ...

When my product name contains slashes, in the html source I see it is url encoded (sometimes, sometimes not). When I urlencoded the product name, the MVC says it is double-encoded and so it fails. When I not - sometimes it is not encoded like this time:

<a href="/Products/Details/green/banana/apple" title="View detail">View</a>

I cannot use product ID here, must use the name. The name may contains slashes and other special characters. They are requirements.

How should I handle this situation on code side? How to configure the route itself, or use some custom encoding-decoding step?

Thanks for your kind advice!

Zoltan Hernyak
  • 989
  • 1
  • 14
  • 35
  • Possible duplicate of [URLs with slash in parameter?](https://stackoverflow.com/questions/6328713/urls-with-slash-in-parameter) – Peter B Mar 05 '19 at 14:11
  • Could you please give us an example of what's outputted when there is a slash and when there is not a slash (i.e. what's being passed to the method when it works and when it doesn't work)? – Kirkland Mar 05 '19 at 14:12
  • When there is a slash, the method is not called as a "HTTP Error 404.0 - Not Found" is raised. When there is no slash, the productname is set in the method parameter. – Zoltan Hernyak Mar 05 '19 at 14:16
  • What I understand the issue to be is; You have a route which points to the Details method on the ProductController class. It's supposed to pass a string called productName, and a integer called revision. You're generating the links with Url.Action(…), and your (correct) expectation is for it to URL encode the parameters. The issue you're seeing is that is sometimes does and sometimes doesn't URL encode the productName parameter, which is causing an issue? – Kirkland Mar 05 '19 at 14:21
  • In which case, my question is what's being passed to Url.Action(…)'s productName parameter when it fails? So what's in the string value of the model at prod.ProductName? – Kirkland Mar 05 '19 at 14:22
  • 1
    @PeterB It seems to me it is not duplicated, as the question talks about "giving the rest as an ID", and I want to separate and handle the productName and the revision number correctly. The wiki id really and directly contains slashes, my links accidentally, and should know and process like this slash is the part of the string value. – Zoltan Hernyak Mar 05 '19 at 14:22
  • When it fails - the slash is not encoded (or in the html source it is not encoded) - nor it should. Without encoding, the url where the link points to a site as it is shown in the question "Products/Details/green/banana/apple", which page does not exists (IIS says). – Zoltan Hernyak Mar 05 '19 at 14:28
  • When I change the link like this: href="@Url.Action("Details" ,"Product", new { productName = @Url.Encode(prod.ProductName), revision = @prod.Revision })" it generates the following html output: <ä href="/Products/Details/green%252fbanana%252fapple/1" but clicking on it raises HTTP Error 404.11 - Not Found The request filtering module is configured to deny a request that contains a double escape sequence. – Zoltan Hernyak Mar 05 '19 at 14:28
  • Try to remove the @ before referencing the model, like the following; – Kirkland Mar 05 '19 at 14:29
  • The name of the product (as string) = "green/banana/apple". – Zoltan Hernyak Mar 05 '19 at 14:29
  • @Kirkland removing the @ makes no difference. It is not encoded then, the generated link looks as – Zoltan Hernyak Mar 05 '19 at 14:31

0 Answers0