views:

46

answers:

3

Just wondering what the best way to do SO style routes is.

http://stackoverflow.com/questions/3459559/what-is-the-best-way-to-do-so-style-routes-dashes-as-spaces

What's the best way to create routes like this?

+1  A: 

I think the best way would be to replace any non-alphanumeric character with a dash. You could search for this regex [^\w]+ and replace with -.

FrustratedWithFormsDesigner
Does this convert multiple spaces into one hyphen?
Dismissile
No, this is a regular expression to find the characters that *will* be replaced by hyphen. Actual code might look like: `strInput = Regex.Replace(strInput,"[^\w]+","-")`
FrustratedWithFormsDesigner
Yeah...I just meant will that regex treat multiple spaces as one. Regexes are like chinese to me :)
Dismissile
Regexes are good to learn. Eventually you will come across problems where a regex solution will be much easier than the non-regex solution. If you want a good place to learn, start with http://www.regular-expressions.info/
FrustratedWithFormsDesigner
+2  A: 

Here's one way.

Here's another.

It's called a slug btw.

Skilldrick
+4  A: 

I've found ASP.NET MVC and Clean SEO Friendly URLs on Dominic Pettifer's blog.

It works on a white-list principle, allowing all 0-9 and a-z characters through, dealing with a few special cases, and converting everything else to -hyphens-. You'll notice that spaces are being converted to hyphens as well, and you might be tempted to use underscores instead. Don't! There are sound SEO benefits for using hyphens in that search engines bots treat them as spaces.

ChrisF
+1 for posting a link to my website :-)
Sunday Ironfoot