nisun
nisun

Reputation: 25

ASP.NET MVC3 - How to configure Url to replace space by dash(-) in Url?

I want to ask how to replace space by dash(-) in my Url. My Details action has a title url, the problem is that appears whitespace between words instead dash(-).

For Examble:

my url : ~/khoa-hoc/1/Lớp Luyện Thi

but I want:
step1-> 1. ~/khoa-hoc/1/Lớp-Luyện-Thi step2-> 2. ~/khoa-hoc/1/lop-luyen-thi

my route

   my route routes.MapRoute( null,
      "khoa-hoc/{id}/{title}",
      new { controller = "Course", action = "Details", id = "", title=""},
      new { id = @"\d+" } );

my view: @Html.ActionLink(item.Name, "Details", new { id = item.CourseID, title=item.Title })

I have search some topics about this but until now I still have not solved this problem, hope someone can help me, Thank in advance, sorry for my english language not perfect.

Upvotes: 2

Views: 1519

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Having arbitrary text in the path portion of an url is something that you might be struggling with. Scott Hanselman explained in details the problems you might encounter if you attempt to do that at the following blog post. At the end of his post here's what he recommends for this scenario:

After ALL this effort to get crazy stuff in the Request Path, it's worth mentioning that simply keeping the values as a part of the Query String (remember WAY back at the beginning of this post?) is easier, cleaner, more flexible, and more secure.

Now this being said you probably want to go against this recommendation and still attempt to use such arbitrary texts in the path portion of your urls. Just like StackOverflow does it with the question titles here. You could use a filtering function to replace all dangerous characters when constructing the slug. Here's how they generate those SEO friendly urls.

Upvotes: 3

Related Questions