问题
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
?
回答1:
How about removing the ? character or replacing it with something else like a dash (-) or underscore (_) ?
回答2:
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.
回答3:
you can create a jquery plugin that check all the links and replace the char that you need to replace with the new value. and after apply this plugin to all the ActionLinks
来源:https://stackoverflow.com/questions/406711/urlencode-of-link-ids-with-asp-net-mvc-htmlhelper-extension-methods