views:

1424

answers:

2

I have actions that take string id parameters that are based on a username which can include characters that require encoding, for instance "user?1"

If I use ActionLink() to generate the links, passing the string without encoding, it generates a link like this: http:\\localhost\controller\action\user?1, and the action gets passed "user" as the id.

If I UrlEncode() the string before passing it to ActionLink, then the link generated is: http:\\localhost\controller\action\user%253f1 as ActionLink will then encode the '%' character for you. Besides this looking ugly, it then also generates a HTTP Error 400 - Bad Request when following the link which I've not yet tracked down the cause of.

Is there any way that I can generate the url like: http:\\localhost\controller\action\user%3f1?

A: 

How about removing the ? character or replacing it with something else like a dash (-) or underscore (_) ?

ajma
The idea is that the url slug is actually a user generated string, in the current case it is their username. I'm unfortunately not in the position of being able to constrain the characters allowed as it is a n existing system with existing users.
Giraffe
The worry with replacing characters is that what if someone tries to register a new account as user-1 or user_1 ? What would their url slug be if I had used one of those for user?1 ?
Giraffe
how about disallowing ? in the username then?
ajma
do you need to allow the '?' in the username
ajma
A: 

You should look in the Global.asax.cs file

add another route for your convenience, in this case, the ff. might work:

        routes.MapRoute(
             null,                            
             "{controller}/{action}/user/{id}",
             new { controller = "Home", action = "Index" }
         );

I guess this is what you want, to separate action for each users, but i suggest you use cookie for this purpose.

PS: Remember to put that one on top of your default route since routing is trying to match from top to bottom.

DucDigital