I am reading in a number of strings which have /
'es through them, the most common example being dates/numbers like 05/06, 07/08, etc.
I am using ASP.NET MVC2 and generating links off of these strings read out of DB, which therein lies the problem.
Obviously a link to:
www.mysite.com/yearperiod/05/06/detail
will not work the same as a link to
www.mysite.com/yearperiod/2005/detail
What is a good solution to this problem? My first thought is to convert every /
to a -
but then I run into the problem/overhead of having to keep track of which ones were converted, so that when writing back to the DB I don't inadvertantly switch a valid -
to a /
(if that makes sense)
edit: come to think of it, I wouldn't know how to keep track of each converted /
... hmmmm.....
edit1: Given this string: "2001/2 - Fruit"
var encodedLinkText = HttpContext.Current.Server.UrlEncode(linkText); //result is "2001%2f2+-+Fruit"
but then if I call that linkhelper from my view the rendered link comes out as :
view source:
<li><a href="2001%2f2+-+Fruit">2001/2 - Fruit</a></li>
rendered link:
http://localhost:XXXX/Period/2001/2+-+Fruit
while I need it as:
http://localhost:XXXX/Period/2001%2f2+-+Fruit
edit2: The browser (testing in chrome) is automatically converting the %2f
to a /
. What now?
edit3: uh oh... looks like IE behaves properly (as required)... hmmm...
- IE displays the link properly (encoded) and after clicking it remains encoded.
- Firefox displays the link decoded and after clicking is encoded.
- Chrome displays link decoded and after clicking is decoded.