2

I need to build an url like this: /products/myproductdescription/5; it works except when the product description contains a "/". I build the link with razor in this way:

<a href="@Url.Content("/Products/" + @product.Description + "/" + @product.Id)">@product.Description</a>

I thought that using @product.Description would encode the description, but I get a link with the "/" if it is present in the description. I tried this way also:

<a href="@Url.Content("/Products/" + @Html.Encode(product.Description) + "/" + @product.Id)">@product.Description</a>

but the result is the same ... Someone can tell me why that part of the link is not encoded? Thank you.

Daniele Armanasco
  • 7,289
  • 9
  • 43
  • 55
  • 2
    Your question doesn't make a lot of sense. Url.Content is used to map a physical path to a url. you can't have /'s in a physical on-disk path, so what exactly are you trying to map to on the physical disk? – Erik Funkenbusch May 29 '12 at 15:19
  • @MystereMan, it's not actually physical. It could be virtual. For example the name of the controller and the action do not physically exist as files on the server. But your point is right. The `/` character is reserved and has nothing to do in the path portion of an url as it has a different meaning. – Darin Dimitrov May 29 '12 at 15:23
  • I'm not using physical path; I'm using Url.Content to build a link that the routing will interpret. I used Url.Content because it resolved me some problem with images (physically present); I understood from your comment that it is not useful here and I remove it. But the problem remains. – Daniele Armanasco May 29 '12 at 15:28
  • @DarinDimitrov - You're right, I misspoke. It's virtual in the sense it can be a virtual directory in IIS. In any case, it's not legal to have a / in a physical or virtual path part, so the Url.Content needs to map to whatever the legal path name is – Erik Funkenbusch May 29 '12 at 15:28
  • @DanieleArmanasco - Url.Content is not used for this purpose. It's used to map to physical content on disk. What you probably want is Url.Action or Url.RouteUrl – Erik Funkenbusch May 29 '12 at 15:31

1 Answers1

1

You should avoid using special characters in the path portion of an url. You could use slugs and replace all dangerous characters. Here's for example how that's done on StackOverflow with the question titles in the url. In this case in order to be able to uniquely identify the resource always use an id. The description could only be used for SEO purposes.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I agree with you; I'm using the description for SEO purposes and I read the id after the description to recover my data from db ... but you're right, I tried to encode manually in the browser the "/" (with %2F) and I saw that the browser interprets it always as "/". So it seems impossible to have a description containing a "/" in the url ... I have to replace it with some character ... – Daniele Armanasco May 29 '12 at 15:39