0

Possible Duplicate:
How does StackOverflow generate its SEO-friendly URLs?

I made a simple form that contains the NAME and some other fields.
When user submits form, the following route will be called:
~/Profile/NAME

If user entered John Smith as NAME, my URL will be:
~/Profile/John%Smith
And I want it to be:
~/Profile/John-Smith

The application will display user details and again the same form at the top of the page.
NAME value will be passed to the form via ViewData.
I want to populate text box with John Smith, not John-Smith.

How can I do that? I ran out of ideas.
Thanks in advance!

EDIT:

I have found solution here: http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/.
It needs a little modification, but basically this is it.

Community
  • 1
  • 1
šljaker
  • 7,294
  • 14
  • 46
  • 80

2 Answers2

1

HttpUtility, look into UrlDecode / UrlEncode

Edit

Use jQuery. Pseudo:

<input type="text" value="name" />
<input type="submit" id="submit" />

The jQuery

$('submit').click(function() {
 $('name').val($('name').val().replace(' ', '-'));
});

It can look something like that, and when you want to display it, you just replace - with space, but remember, there are names that contain - and should not be replaced.

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • `UrlEncode` will not replace `%20` with dash or the other way round with `UrlDecode`. – Darin Dimitrov Feb 18 '10 at 08:16
  • Correct. And I mentioned that I need it for ASP.NET MVC, not classic web forms. – šljaker Feb 18 '10 at 08:25
  • Ah I miss read, i thought that he wanted to encode the url and the decode it to get "John Smith" out of "John%Smith". – Filip Ekberg Feb 18 '10 at 08:25
  • @OP, you can still use HttpUtility even if it's not Web Forms. – Filip Ekberg Feb 18 '10 at 08:30
  • Can I solve the problem by using Route class? This solution will not work if the user switched off java script. – šljaker Feb 18 '10 at 13:02
  • I don't think you can use Routing for this.. You can probably use some special routing and replace it before it's passed down to the controller.. But more than that, I don't know you have to try. Just wondering, why do you want to have a "javascript turned off"-solution? Does people still turn js off?... – Filip Ekberg Feb 18 '10 at 13:24
0

If it helps any, I used this from Jeff Atwood as well as the link you garnered above to make it happen.

Community
  • 1
  • 1
George Stocker
  • 57,289
  • 29
  • 176
  • 237